Hibernate Criteria with group by getting error
我有这两个引用表的实体 java 类(jos_content
- 您将需要像这里这样的结果转换器,或者只是期望结果不会是 ContentEntity - 而是投影列的数组。正是异常所说的:"结果是 int (content.id) ...未转换为 ContentEntity
-
嗨,Radim Kohler,您能简要解释一下吗?
-
嗨 Radim Kohler,我用过这个:PropertyProjection propProjection = Projections.groupProperty("content.id"); contentCriteria = contentCriteria.setProjection(propProjection); contentCriteria = contentCriteria.setResultTransformer(Transformers.aliasToBea??n(ContentEntity.class????s));并得到所有的值都是空的。
我们可以使用投影来缩小选定列的范围,但是我们必须决定
-
我们将使用表示为 object[] 的结果吗
-
我们会将结果转换为 DTO(如果可能的话,转换为原始实体)
如果我们要进行转换,我们必须帮助转换器使用列名。它通过传递 alias
完成
1 2 3 4 5 6 7 8 9 10
| PropertyProjection propProjection = Projections
.groupProperty("content.id")
.as("id") // the alias
;
contentCriteria
.setProjection(propProjection)
.setResultTransformer(Transformers.aliasToBean(ContentEntity.clas????s));
List<ContentEntity> groupedEntities = contentCriteria.list(); |
那么结果将是 ContentEntity 的列表,该列表将仅填充 id。请参阅:
The alias() and as() methods simply wrap a projection instance in another, aliased, instance of Projection.
或者我们可以预期结果为 object[]
1
| Object[] results = contentCriteria. list(); |
扩展:
如果我们想获取路由实体列表,我们可以将当前查询转换为 DetachedCriteria
1 2 3 4 5
| DetachedCriteria grouped = DetachedCriteria.forClass(ContentEntity.class,"grouped")
// Filter the Subquery
.add(...
// SELECT The User Id
.setProjection(propProjection) |
所以,上面只会返回我们喜欢的ID,主查询会被它们过滤,同时返回完整的对象(无需转换)
1 2
| Criteria query = session.createCriteria(ContentEntity.class,"content")
.add( Subqueries.propertyIn("content.id", grouped) ); |
在这里检查类似的东西:Hibernate Criteria for "in subselect"
- 现在有什么例外?它不是像以前那样的 ClassCastException 吗?
-
它工作但还不够,因为我想获取定义到 ContentEntity.java 文件中的所有字段。对于上述解决方案,我只得到一个字段值,即 ID。此外,我的查询已更改,其显示没有连接。
-
结果查询是:从 jos_content this_ where this_.id=? select this_.id as y0_而 this_.state=?按 this_.id 分组
-
确切地。这就是您定义的标准 - 仅返回组的 id。如果您想要完整的实体 - 您必须将此标准用作分离的标准,并将它们用于子查询
-
this_.large_kicker_image as large9_0_1_, this_.large_kicker_image_alt_text as large10_0_1_, this_.fulltext as fulltext0_1_, this_.sef_url as sef12_0_1_, this_.metadesc as metadesc0_1_, this_.metakey as metakey0_1_, this_.kicker_image_caption as kicker15_0_1_, jos.articles_1_, jos3_article id 为 id3_, josarticle2_.id 为 id1_0_, josarticle2_.ordering 为 ordering1_0_, josarticle2_.article_id 为 article3_1_0_ from jos_content this_ left outer join jos_article_section josarticle2_ on this_.id=josarticle2_.article_id where this_.id=?而 this_.state=?按 this_.id 分组
-
请,在这里你可以看到如何使用子查询 stackoverflow.com/a/20427782/1679310
-
你能帮帮我吗,我该怎么做那个超然的标准。
-
我的实际 SQL 查询是 SELECT c.id, c.sef_url, FROM jos_content c, jos_article_section a WHERE c.id = ? a.article_id=c.id and c.state=1 group by c.id and expected required: select this_.id as id0_1_, this_.sef_url as sef12_0_1_ from jos_content this_ left outer join jos_article_section josarticle2_ on this_.id=josarticle2_。 article_id this_.id=?而 this_.state=?我该怎么做,请帮忙?
-
让我检查您的更新答案,并会回复您。
-
看,我几乎无能为力了。您应该考虑查询应该是什么样子。如果你需要 GROUP BY... 那么你需要预测。但子查询也有帮助。您必须创建内部查询,然后通过它过滤根查询。根查询,以后可以包含一些关联/连接......但我试图回答你的例外:int不能被视为ContentEntity ..祝Hibernate好运