我正在使用带有Hibernate的JPA 2来构建一个查询,这个查询在我的生活中不能正常运行。查询如下:
public Integer getCountForDAR(Date _SD, Date _ED, Integer _PostId, String _Filter) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(TcDarActivities.class);
Root fromTcDarActivities = cq.from(TcDarActivities.class);
Path typePath = fromTcDarActivities.get(BaseDao.TC_DAR_ACTIVITY_TYPE).get(BaseDao.TYPE_NAME);
Path postPath = fromTcDarActivities.get(BaseDao.TC_POST).get(BaseDao.POST_ID);
cq.select(fromTcDarActivities).where(
cb.and(cb.between(fromTcDarActivities.get(BaseDao.DARACTIVITY_TIME), _SD, _ED),
cb.equal(postPath, _PostId),
cb.or(
cb.like(fromTcDarActivities.get(BaseDao.COMMENTS), _Filter),
cb.like(typePath, _Filter)
)));
TypedQuery tq = getEm().createQuery(cq);
LOGGER.info(tq.unwrap(org.hibernate.Query.class).getQueryString());
try
{
List<TcDarActivities> list = getEm().createQuery(cq).setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS).getResultList();
return list.size();
}
catch(Exception e) { return 0; }
}
TC_DAR_ACTIVITY_TYPE字段在数据库中可以为null。问题是,如果TC_DAR_ACTIVITY_TYPE和COMMENTS具有日期范围内的值,则查询仅返回结果。有人可以帮忙吗?我正在解决这个问题,因为我很确定我做得对...
由于 韦塞尔
答案 0 :(得分:0)
类型和发布路径使用规范要求的内部联接。您必须为要使用左外连接的任何连接指定连接和joinType.LEFT。
FromTcDarActivities.join(BaseDao.TC_DAR_ACTIVITY_TYPE, JoinType.LEFT).get(BaseDao.TYPE_NAME);