DynamicQuery 父子错误 java.sql.exception 操作数应包含一列
DynamicQuery parent-child error java.sql.exception Operand should contain one column(s)
我正在尝试编写以下查询代码:
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);
It returns me java.sql.SQLException:操作数应包含 1 列,因为生成的子查询 select 版本和 articleId:
select max(child_.version) as y0_, child_.articleId as y1_ from JournalArticle child_ where child_.articleId=this_.articleId group by child_.articleId
有人可以帮助我吗?
谢谢
问题出在子查询的构造中。原来我是这样编码的:
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);
但是这个动态查询总是select最大版本和articleId,所以我这样改了
DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
childQuery.add(PropertyFactoryUtil.forName("articleId").eqProperty("parent.articleId")).setProjection(ProjectionFactoryUtil.max("version"));
就这些了
我正在尝试编写以下查询代码:
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);
It returns me java.sql.SQLException:操作数应包含 1 列,因为生成的子查询 select 版本和 articleId: select max(child_.version) as y0_, child_.articleId as y1_ from JournalArticle child_ where child_.articleId=this_.articleId group by child_.articleId
有人可以帮助我吗? 谢谢
问题出在子查询的构造中。原来我是这样编码的:
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);
但是这个动态查询总是select最大版本和articleId,所以我这样改了
DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
childQuery.add(PropertyFactoryUtil.forName("articleId").eqProperty("parent.articleId")).setProjection(ProjectionFactoryUtil.max("version"));
就这些了