Hibernate Criteria - 从select中排除groupProperty [英] Hibernate Criteria - Exclude groupProperty from select
本文介绍了Hibernate Criteria - 从select中排除groupProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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会自动将该属性添加到子查询中的select子句,而我找不到任何方法来阻止这种情况的发生。
结果当然是DB错误,因为子查询返回的值太多。
有没有人知道解决这个问题的方法?
解决方案
DetachedCriteria子查询= 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();
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?
解决方案
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();
这篇关于Hibernate Criteria - 从select中排除groupProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文