Search code examples
sqlhibernateliferaysqlexceptiondynamicquery

DynamicQuery parent-child error java.sql.exception Operand should contain one column(s)


I'm trying to code the following query:

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
---

I coded it this way:

    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: Operand should contain 1 column(s) because the generated child query select version and articleId: select max(child_.version) as y0_, child_.articleId as y1_ from JournalArticle child_ where child_.articleId=this_.articleId group by child_.articleId

Can someone help me? Thanks


Solution

  • The problem was in the construction of the child query. In origin I coded it this way:

    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);
    

    but this dynamic query always select max version and articleId, so I changed it this way

    DynamicQuery childQuery = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "child", classLoader);
    childQuery.add(PropertyFactoryUtil.forName("articleId").eqProperty("parent.articleId")).setProjection(ProjectionFactoryUtil.max("version"));
    

    That's all