JPA criteribuilder count返回错误的数据



我想计算我的行条件birthId,我从AnimalFilter和deleteDatetime总是空的。它返回一些奇怪的值->相似的每个出生id,但他们在我的数据库是不同的。我做错了什么?

我代码:

@Repository
public class AnimalSearchCount {
@PersistenceContext
private EntityManager entityManager;
Long countBy(AnimalFilter filter) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<Animal> animal = cq.from(Animal.class);
Predicate deleteDatetimePredicate = cb.isNull(animal.get("deleteDateTime"));
if (filter.getBirthId() != null) {
cq.select(cb.count(animal.get("birth").get("id")));
cq.where(deleteDatetimePredicate);
}
return entityManager.createQuery(cq).getSingleResult();
}
}
if (filter.getBirthId() != null) {
cq.select(cb.count(animal));
cq.where(
deleteDatetimePredicate, 
cb.eq(animal.get("birth").get("id"), filter.getBirthId()));
}

相关内容

  • 没有找到相关文章

最新更新