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
Lucas
  • 1
  • 2
  • 1
    Is there more to your criteria? From that error it seems like you are getting back a Long value and not a mapped entity. What are your projecting? It looks like you just have a group by. You don't seem to have an aggregate with your group by. – Arthur Thomas Mar 30 '10 at 21:07
  • I put the criteria above. It has more lines but they don't matter here, they are just another restrictions. I don't know if it has a better way to group data using hibernate criteria. – Lucas Mar 30 '10 at 21:22
  • ok, sorry, but do you mind putting the SQL for what you are trying to do? I don't understand from your criteria example. Are you putting in Class.class for your entity class parameter in .forClass? Well with projections you usually have something you are projecting (the select statement part) and you only have a group by with no aggregate function (which usually accompanies a group by). – Arthur Thomas Mar 30 '10 at 21:28
  • I put the SQL for what i'm trying to do above. I put the "Class.class" just as an example. :) – Lucas Mar 30 '10 at 21:34

1 Answers1

1

ok if you are just wanting to project the unique id of an event then you do not need the group by. Just use Projections.id(). Grouping by a unique id is just going to give you this list of ids anyway.

If you have unique idents 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and you have a query that returns 1, 2, 3, 4 and tell it to group by the id then it is going to make 4 'groups' since those are unique ids :)

edit: well the project list and a collection of projection objects so you can append as many projection items as you want on there. Just keep adding them. I believe you use Projections.property("property_name").

edit2: also which what you are doing you don't really need criteria. you could use HQL.

Arthur Thomas
  • 5,088
  • 1
  • 25
  • 31