I want to count my rows with conditions birthId which I have from AnimalFilter and deleteDatetime which is always null. It returns me strange values -> similar for every birthId but they are different in my database. What am I doing wrong?
My code:
@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()));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.