Hibernate criteria - exclude groupProperty from the selected

I would like to use the sleep criteria criteria object as a subquery for the second criterion, for example:

    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 groupProperty automatically adds this property to the select clause in the subquery request, and I cannot find a way to stop this.

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

Does anyone know about this?

+3
source share
1 answer

Try for example the sample below,

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();
+1
source

Source: https://habr.com/ru/post/1763236/


All Articles