【问题标题】:Hibernate Criteria - Exclude groupProperty from selectHibernate Criteria - 从选择中排除 groupProperty
【发布时间】:2011-04-08 05:55:17
【问题描述】:

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

    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 子句中,我找不到任何方法来阻止这种情况发生。

结果,当然是数据库错误,因为子查询返回的值太多。

有人知道解决这个问题的方法吗?

【问题讨论】:

  • 还是没有答案? :'(

标签: java hibernate criteria


【解决方案1】:

试试下面的示例,

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 个键)并取最大值,最后它成为组本身。
猜你喜欢
  • 2023-03-28
  • 2012-06-20
  • 1970-01-01
  • 2013-05-10
  • 2013-07-24
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多