Java Hibernate中的条件相关子查询

Java Hibernate中的条件相关子查询,java,mysql,hibernate,criteria,criteria-api,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

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

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'))`
这将引发以下异常:

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

谁能告诉我我做错了什么

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);`