使用Hibernate预测的问题

时间:2010-03-30 20:56:46

标签: hibernate jsf richfaces detachedcriteria

我正在使用Richfaces + HibernateQuery来创建数据列表。我正在尝试使用Hibernate Projections对查询结果进行分组。这是代码:

final DetachedCriteria criteria = DetachedCriteria
     .forClass(Class.class, "c")
     .setProjection(Projections.projectionList()
     .add(Projections.groupProperty("c.id")));
     ...

在.xhtml文件中我有以下代码:

  <rich:dataTable width="100%" id="dataTable" value="#{myBean.dataModel}" var="row">
<f:facet name="header">
 <rich:columnGroup>
                 ......
 </rich:columnGroup>
</f:facet>
<h:column>
 <h:outputText value="#{row.id}"/>
</h:column>
<h:column>
 <h:outputText value="#{row.name}"/>
</h:column>

但是当我运行页面时,它会给我以下错误:

Error: value="#{row.id}": The class 'java.lang.Long' does not have the property 'id'.

如果我从代码中取出Project它可以正常工作,但它不会对结果进行分组。那么,哪个错误可能会发生在这里?

编辑:以下是完整标准:

final DetachedCriteria criteria = DetachedCriteria.forClass(Event.class, "e");

...
        joins....
...

criteria.setProjection(Projections.distinct(Projections.projectionList()
        .add(Projections.groupProperty("e.id").as("e.id"))));

getDao().findByCriteria(criteria);

如果我采用“setProjection”行,它可以正常工作。我不明白为什么它会把这一行给出错误。

以下是我正在尝试的查询:

select e.event_id from event e
inner join event_product_group epg
  on e.event_id = epg.event_id
inner join product_group pg
  on pg.product_group_id = epg.product_group_id
where pg.description like '%TEXT%'
group by e.event_id

1 个答案:

答案 0 :(得分:1)

好的,如果您只是想要投影事件的唯一ID,那么您不需要分组。只需使用Projections.id()。按唯一ID分组,无论如何都会给你这个ID列表。

如果您有唯一的标识符1,2,3,4,5,6,7,8,9,10,并且您有一个返回1,2,3,4的查询并告诉它按ID进行分组然后它将成为4个'组',因为它们是独特的ID:)

编辑:以及项目列表和投影对象的集合,以便您可以在此处追加任意数量的投影项目。继续添加它们。我相信你使用Projections.property(“property_name”)。

edit2:你正在做什么,你真的不需要标准。你可以使用HQL。