views:

33

answers:

1

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  A: 

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