我想使用hibernate criteria对象作为第二个条件的子查询,如下所示:

    DetachedCriteria latestStatusSubquery = DetachedCriteria.forClass(BatchStatus.class);
    latestStatusSubquery.setProjection(Projections.projectionList()
            .add( Projections.max("created"), "latestStatusDate")
            .add( Projections.groupProperty("batch.id"))
    );

    DetachedCriteria batchCriteria = DetachedCriteria.forClass(BatchStatus.class).createAlias("batch", "batch");
    batch.add( Property.forName( "created" ).eq( latestStatusSubquery ) );

问题是,添加groupproperty会自动将该属性添加到subselect查询的select子句中,我找不到任何方法来阻止这种情况的发生。
结果,当然是db错误,因为子查询返回的值太多。
有人知道怎么解决这个问题吗?

最佳答案

试试下面的例子,

DetachedCriteria subquery = DetachedCriteria.forClass(CustomerCommentsVO.class, "latestComment");
            subquery.setProjection(Projections.max("latestComment.commentId"));
            subquery.add(Expression.eqProperty("latestComment.prospectiveCustomer.prospectiveCustomerId", "comment.prospectiveCustomer.prospectiveCustomerId"));

 objCriteria = objSession.createCriteria(CustomerCommentsVO.class,"comment");
            objCriteria.add(Subqueries.propertyEq("comment.commentId", subquery));
List lstComments = objCriteria.list();

10-08 02:24