我不是 JPA 持久性标准 API 专家,有时我使用它会感到非常头疼。
昨天我注意到一个新的非常奇怪的行为。我将发布的代码是对现有功能代码的改编,因此不要关注琐碎的错误。我正在使用 glassfish 3.1.1 和相应的 eclipse 持久性插件和 Mysql DB。
我写了一个criteriaQuery,它过滤来自不同表的数据。如果第二次执行此条件查询两次,则会生成错误的 SQL 查询。我不知道为什么。
public CriteriaQuery createQuery4Count(EntityManager em) {
Calendar lastDate4Search = GregorianCalendar.getInstance();
javax.persistence.criteria.CriteriaBuilder cb = em.getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq = cb.createQuery();
javax.persistence.criteria.Root<Permessimercepath> checkPointRt = cq.from(Permessimercepath.class);
javax.persistence.criteria.Path<Permessimerce> permessimerceClass = checkPointRt.get(Permessimercepath_.permessimerce);
Predicate checkPointDatePredicate = cb.isNull(checkPointRt.get(Permessimercepath_.dataTransito));
Predicate checkPointAreaPredicate = cb.equal(checkPointRt.get(Permessimercepath_.iDArea), area);
Predicate datePredicate = cb.greaterThanOrEqualTo(permessimerceClass.get(Permessimerce_.datafine), lastDate4Search.getTime());
Predicate isValidPredicate = cb.lt(permessimerceClass.get(Permessimerce_.statopermesso), Permessimerce.COMPLETED);
cq.where(cb.and(checkPointAreaPredicate, checkPointDatePredicate, datePredicate, isValidPredicate));
cq.select(cb.countDistinct(checkPointRt));
return cq;
}
CriteriaQuery myCriteriaQuery = createQuery4Count(getEntityManager())
javax.persistence.Query q = getEntityManager().createQuery(myCriteriaQuery );
Long Result = ((Long) q.getSingleResult()).intValue();
// second query created with the same criteriaQuery
q = getEntityManager().createQuery(myCriteriaQuery );
Long Result2 = ((Long) q.getSingleResult()).intValue();
生成的sql是
// First and correct one
SELECT COUNT(t0.ID_permesso) FROM permessimercepath t0 WHERE EXISTS (SELECT t1.ID_permesso FROM permessimerce t2, permessimercepath t1 WHERE ((((t0.ID_permesso = t1.ID_permesso) AND (t0.CheckPointIndex = t1.CheckPointIndex)) AND ((((t1.ID_Area = ?) AND (t1.DataTransito IS NULL)) AND (t2.Data_fine >= ?)) AND (t2.Stato_permesso < ?))) AND (t2.ID_permesso = t1.ID_permesso)))
bind => [3 parameters bound]
// Second and wrong one
SELECT COUNT(t0.ID_permesso) FROM permessimercepath t0, permessimerce t2, permessimercepath t1 WHERE (((((t1.ID_Area = ?) AND (t1.DataTransito IS NULL)) AND (t2.Data_fine >= ?)) AND (t2.Stato_permesso < ?)) AND (t2.ID_permesso = t1.ID_permesso))
如果没有人知道它为什么会发生,我可以尝试以更简单的方式重现它。
谢谢菲利波