CriteriaQuery JPA2问题及结果



我使用JPA2和Hibernate来构建一个在我的一生中都无法正确运行的查询。以下查询:

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的值在日期范围内时,查询才会返回结果。有人能帮忙吗。我对此感到头疼,因为我很确定我做得对。。。

谢谢Wessel

Type和post-path使用规范要求的内部联接。必须为要使用左外部联接的任何联接指定联接和联接类型.LEFT。

FromTcDarActivities.join(BaseDao.TC_DAR_ACTIVITY_TYPE, JoinType.LEFT).get(BaseDao.TYPE_NAME);

相关内容

  • 没有找到相关文章

最新更新