标准与Hibernate中的子查询相关

时间:2016-02-18 11:54:06

标签: java mysql hibernate criteria criteria-api

我正在尝试编写以下查询:

Select b.balance, a.acctOwnerId from
AcctBalHist b, Acct a
  where a.acctOwnerId in ('id1','id2','id3')
  and b.acctNbr = a.acctNbr
  and b.created = (Select max(b2.created) 
                  from AcctbalHist b2 
                  where b2.acctNbr = a.acctNbr 
                  and b2.created < date('2012-02-02'))`

Hibernate中使用相关子查询。

这就是我所拥有的:

CriteriaBuilder builder = hiby().getCriteriaBuilder();
    CriteriaQuery<IdAmountWrapper> criteria = builder.createQuery(IdAmountWrapper.class);

    Root<AcctBalHist> acctBalHistRoot = criteria.from(AcctBalHist.class);
    Join<AcctBalHist, Acct> acctBalHistAcctJoin = acctBalHistRoot.join(AcctBalHist_.acct);

    Subquery<Date> subqueryCriteria = criteria.subquery(Date.class);
    Root<AcctBalHist> subAcctBalHistRoot = subqueryCriteria.from(AcctBalHist.class);
    Join<AcctBalHist, Acct> balHistRoot = subqueryCriteria.correlate(acctBalHistAcctJoin);
    balHistRoot.on(builder.equal(subAcctBalHistRoot.get(AcctBalHist_.acct), balHistRoot.get(Acct_.acctNbr)));

    subqueryCriteria.select(builder.greatest(subAcctBalHistRoot.get(AcctBalHist_.created)))
            .where(builder.and
                   (builder.lessThan(subAcctBalHistRoot.get(Actv_.created), '2012-02-02')));

    criteria.select(builder.construct(IdAmountWrapper.class,
            builder.abs(acctBalHistRoot.get(AcctBalHist_.balance)),
            acctBalHistAcctJoin.get(Acct_.acctOwnerId)))
            .where(builder.and(
                    acctBalHistAcctJoin.get(Acct_.acctOwnerId).in(workerIds),
                    builder.in(acctBalHistRoot.get(AcctBalHist_.created)).value(subqueryCriteria)));

    TypedQuery<IdAmountWrapper> query = hiby().createQuery(criteria);`

引发以下异常:

  

java.lang.ClassCastException:org.hibernate.hql.internal.ast.tree.SqlNode无法强制转换为org.hibernate.hql.internal.ast.tree.FromReferenceNode

谁能告诉我我做错了什么?

0 个答案:

没有答案