使用 Hibernate 投影时出现问题
我正在使用 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'.
如果我从代码中取出投影,它会正常工作,但不会对结果进行分组。那么,这里可能发生哪个错误?
编辑:这是完整的标准:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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。
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.