Hibernate Criteria - Exclude groupProperty from select

我只是一个虾纸丫 提交于 2019-12-22 10:27:45

问题


I would like to use a hibernate criteria object as a subquery on a second criteria, like this:

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

The problem is that adding a groupProperty automatically add that property to the select clause on the subselect query and I can't find any way to stop this from happening.

The result, of course a DB error because the subquery returns too many values.

Does anyone know a way around this?


回答1:


Try like below sample,

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


来源:https://stackoverflow.com/questions/3642688/hibernate-criteria-exclude-groupproperty-from-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!