OpenJPA Criteria Query API - 子查询在生成的SQL中创建额外的根

时间:2014-04-29 11:46:17

标签: java java-ee jpa openjpa criteria-api

我正在使用WebSphere Application Server v8.0.0.8提供的OpenJPA 2.1.2-SNAPSHOT。

我正在尝试生成以下SQL:

SELECT t0.id, t0.active, t0.CREATE_DATE, t0.FIRST_NAME, t0.LAST_NAME, 
    t0.UPDATE_DATE, t3.USER_ID, t3.id, t3."COUNT", t3.COUNT_DATE, 
    t3.CREATE_DATE, t4.id, t4.address, t4.city, t4.country, 
    t4.CREATE_DATE, t4.name, t4.NUM, t4."STATE", t4.UPDATE_DATE, 
    t4.zip, t5.id, t5.CODE_ID, t5.CREATE_DATE, t5.EXPIRATION_DATE, 
    t5.UPDATE_DATE, t6.id, t6.CODE_ID, t6.CREATE_DATE, 
    t6.description, t6.name, t6.SALES_TEAM, t6.UNIT_OF_MEASURE, 
    t6.UPDATE_DATE, t3.LOCATION_NUM, t3.LOT_CODE, t3.PRODUCT_CODE, 
    t3.UPDATE_DATE 
FROM CONSIGNMENT.HC_USER t0, CONSIGNMENT.HC_COUNT t3, CONSIGNMENT.HC_LOCATION t4, 
    CONSIGNMENT.HC_LOT t5, CONSIGNMENT.HC_PRODUCT t6 
WHERE (t0.id = 'someuser@users.com' AND t3.COUNT_DATE = (SELECT MAX(t2.COUNT_DATE) FROM 
    CONSIGNMENT.HC_COUNT t2)) AND t0.id = t3.USER_ID AND 
    t3.LOCATION_NUM = t4.NUM AND t3.LOT_CODE = t5.CODE_ID AND 
    t3.PRODUCT_CODE = t6.CODE_ID 
ORDER BY t3.USER_ID ASC;

但是,我无意中生成了以下SQL:

SELECT t0.id, t0.active, t0.CREATE_DATE, t0.FIRST_NAME, t0.LAST_NAME, 
    t0.UPDATE_DATE, t3.USER_ID, t3.id, t3."COUNT", t3.COUNT_DATE, 
    t3.CREATE_DATE, t4.id, t4.address, t4.city, t4.country, 
    t4.CREATE_DATE, t4.name, t4.NUM, t4."STATE", t4.UPDATE_DATE, 
    t4.zip, t5.id, t5.CODE_ID, t5.CREATE_DATE, t5.EXPIRATION_DATE, 
    t5.UPDATE_DATE, t6.id, t6.CODE_ID, t6.CREATE_DATE, 
    t6.description, t6.name, t6.SALES_TEAM, t6.UNIT_OF_MEASURE, 
    t6.UPDATE_DATE, t3.LOCATION_NUM, t3.LOT_CODE, t3.PRODUCT_CODE, 
    t3.UPDATE_DATE 
FROM CONSIGNMENT.HC_USER t0, CONSIGNMENT.HC_COUNT t1, 
    CONSIGNMENT.HC_COUNT t3, CONSIGNMENT.HC_LOCATION t4, 
    CONSIGNMENT.HC_LOT t5, CONSIGNMENT.HC_PRODUCT t6 
WHERE (t0.id = 'someuser@users.com' AND t1.COUNT_DATE = (SELECT MAX(t2.COUNT_DATE) FROM 
    CONSIGNMENT.HC_COUNT t2)) AND t0.id = t3.USER_ID AND 
    t3.LOCATION_NUM = t4.NUM AND t3.LOT_CODE = t5.CODE_ID AND 
    t3.PRODUCT_CODE = t6.CODE_ID 
ORDER BY t3.USER_ID ASC;

以下是生成错误SQL的代码:

public HcUser getUserUsingJPA(String userID) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<HcUser> cq = cb.createQuery(HcUser.class);
    Metamodel m = em.getMetamodel();
    EntityType<HcUser> _hcUser = m.entity(HcUser.class);
    EntityType<HcCount> _hcCount = m.entity(HcCount.class);
    Root<HcUser> hcUser = cq.from( _hcUser );
    Root<HcCount> hcCount = cq.from( _hcCount );
    cq.select(hcUser);
    Predicate predicate = cb.equal(hcUser.get(HcUser_.id), userID);

    Subquery<Date> sq = cq.subquery(Date.class);
    Root<HcCount> hcCount2 = sq.from( _hcCount );
    sq.select(cb.greatest(hcCount2.get(HcCount_.countDate)));
    Predicate datePredicate = cb.equal(hcCount.get(HcCount_.countDate), sq);

    cq.where(cb.and(predicate, datePredicate));
    hcUser.fetch( HcUser_.hcCounts );
    hcCount.fetch( HcCount_.hcLocation );
    hcCount.fetch( HcCount_.hcProduct );
    hcCount.fetch( HcCount_.hcLot );
    TypedQuery<HcUser> query = em.createQuery(cq);

    List<HcUser> users = query.getResultList();
    if (users.size() > 0) {
        user = users.get(0);
    }
    return user;
}

我只需要摆脱t1表/别名,并在WHERE子句中的一个地方将t1更改为t3。

当我使用Join而不是2nd Root加上提取时,查询不会向FROM部分添加第二个HcCount。但是,我需要获取方法。

我做错了什么?

0 个答案:

没有答案