0

I'm using Richfaces + HibernateQuery to create a data list. I'm trying to use Hibernate Projections to group my query result. Here is the code:

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

in the .xhtml file i have the following code:

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

But when i run the page it gives me the following error:

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

If i take out the Projection from the code it works correctly, but it doesn't group the result. So, which mistake could be happening here?

EDIT: Here is the full criteria:

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

if i take the "setProjection" line it works fine. I don't understand why it gives that error putting that line.

Here is the query i'm trying to do:

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
4

1 回答 1

1

好的,如果您只是想投影事件的唯一 ID,那么您不需要 group by。只需使用 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")。

编辑2:你在做什么你也不需要标准。你可以使用 HQL。

于 2010-03-30T21:45:43.977 回答