具有动态查询的Express Complex子选择

时间:2013-06-17 13:07:09

标签: liferay dynamicquery

如何在ServiceImpl类中使用等效的Liferay DynamicQuery表达式复制此SQL(子选择):

SELECT * FROM journalarticle
WHERE (urlTitle,version) IN
( SELECT 
    urlTitle,MAX(version) 
FROM journalarticle 
WHERE structureId = 'structure-id' AND companyId = 10150 AND groupId = 10170
GROUP BY urlTitle ) 
ORDER BY createDate DESC 
LIMIT 0,4

1 个答案:

答案 0 :(得分:0)

由于我不能写太长的评论,我在这里粘贴代码会导致你想要的东西,我没有编译或运行它。

DynamicQuery dynamicQuery=DynamicQueryFactoryUtil.forClass(JournalArticle.class);
        dynamicQuery.addOrder(OrderFactoryUtil.desc("createDate"));
        dynamicQuery.setLimit(0, 4);

        DynamicQuery subQuery=DynamicQueryFactoryUtil.forClass(JournalArticle.class);
        subQuery.setProjection(ProjectionFactoryUtil.projectionList().add(ProjectionFactoryUtil.property("_id")).add(ProjectionFactoryUtil.max("version")));
        subQuery.add(PropertyFactoryUtil.forName("structureId ").eq("structure-id"));
        subQuery.add(PropertyFactoryUtil.forName("companyId").eq("10150"));
        subQuery.add(PropertyFactoryUtil.forName("groupId").eq("10170"));
        List<Long> ids=new ArrayList<Long>();
        try {
            List<Object[]> list= JournalArticleLocalServiceUtil.dynamicQuery(subQuery);

            for(Object[] object:list){
                //0th field would be _id
                ids.add((Long)object[0]);
            }
        } catch (SystemException e) {
            // TODO Auto-generated catch block
        }

        dynamicQuery.add(PropertyFactoryUtil.forName("_id").in(ids.toArray()));

        try {
            List<JournalArticle> journalArticles=JournalArticleLocalServiceUtil.dynamicQuery(dynamicQuery);
        } catch (SystemException e) {
            // TODO Auto-generated catch block
        }

我希望这对你有用。