我试图对以下查询进行编码:
select * from mySchema.journalArticle parent
where parent.structureId in (?) and
parent.companyId = ? and
parent.groupId = ? and
parent.status = ? and
parent.version in (
select max(version) from mySchema.JournalArticle child
where child.articleId = parent.articleId group by articleId
)
order by parent.modifiedDate desc
---我是这样编码的:
DynamicQuery parentQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "parent", classLoader);
parentQuery.add(PropertyFactoryUtil.forName("parent.structureId").in(structureIdSet));
parentQuery.add(PropertyFactoryUtil.forName("parent.companyId").eq(companyId));
parentQuery.add(PropertyFactoryUtil.forName("parent.groupId").eq(groupId));
parentQuery.add(PropertyFactoryUtil.forName("parent.status").eq(status));
DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
projectionList.add(ProjectionFactoryUtil.max("child.version"));
projectionList.add(ProjectionFactoryUtil.groupProperty("child.articleId"));
childQuery.add(RestrictionsFactoryUtil.eqProperty("child.articleId","parent.articleId"));
childQuery.setProjection(projectionList);
parentQuery.add(PropertyFactoryUtil.forName("parent.version").in(childQuery));
Order dateOrder = OrderFactoryUtil.desc("parent.modifiedDate");
parentQuery.addOrder(dateOrder);它返回me java.sql.SQLException: Operand应该包含1列,因为生成的子查询选择版本和articleId: select max(child_.version)作为y0_,child_.articleId作为y1_来自JournalArticle child_,child_.articleId=this_.articleId组按child_.articleId
有人能帮我吗?谢谢
发布于 2016-11-14 09:08:49
问题在于子查询的构造。在原点上,我是这样编码的:
DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
projectionList.add(ProjectionFactoryUtil.max("version"));
projectionList.add(ProjectionFactoryUtil.groupProperty("articleId"));
childQuery.add(PropertyFactoryUtil.forName("child.articleId").eqProperty("parent.articleId"));
childQuery.setProjection(projectionList);但是这个动态查询总是选择max版本和articleId,所以我就这样修改了它。
DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
childQuery.add(PropertyFactoryUtil.forName("articleId").eqProperty("parent.articleId")).setProjection(ProjectionFactoryUtil.max("version"));就这样
https://stackoverflow.com/questions/40577996
复制相似问题