留言板

Dynamic Query help

thumbnail
Sandeep Nair,修改在14 年前。

Dynamic Query help

Liferay Legend 帖子: 1744 加入日期: 08/11/6 最近的帖子
Hi everyone,

I need a help in Dynamic Query or with anything that can solve this. I have been using jdbc for writing complex queries. I want to know if this is possible using DynamicQuery or any api in liferay

I have following three tables

OrgType
orgtypeid
orgtypename
isActive

Location
locationid
location_name
isActive

CustomOrg
orgId
orgtypeid
locationId
orgname

I want to find all the organizations whose location and orgType are active. Is it possible using DynamicQuery or any liferayapi

Regards,
Sandeep
thumbnail
jelmer kuperus,修改在14 年前。

RE: Dynamic Query help

Liferay Legend 帖子: 1191 加入日期: 10/3/10 最近的帖子
Something like this should work :


DynamicQuery q = DynamicQueryFactoryUtil.forClass(CustomOrg.class, PortalClassLoaderUtil.getClassLoader())
    .add(PropertyFactoryUtil.forName("orgtypeid")
            .in(DynamicQueryFactoryUtil.forClass(OrgType.class, PortalClassLoaderUtil.getClassLoader())
                    .add(PropertyFactoryUtil.forName("isActive").eq(true))
                    .setProjection(ProjectionFactoryUtil.property("orgtypeid"))
            )
    )
    .add(PropertyFactoryUtil.forName("locationid")
            .in(DynamicQueryFactoryUtil.forClass(Location.class, PortalClassLoaderUtil.getClassLoader())
                    .add(PropertyFactoryUtil.forName("isActive").eq(true))
                    .setProjection(ProjectionFactoryUtil.property("locationid"))
            )
    )
thumbnail
Sandeep Nair,修改在14 年前。

RE: Dynamic Query help

Liferay Legend 帖子: 1744 加入日期: 08/11/6 最近的帖子
Thanks Jelmer.
thumbnail
Antonio Musarra,修改在12 年前。

RE: Dynamic Query help

Junior Member 帖子: 66 加入日期: 11/8/9 最近的帖子
Hi Jelmer,
How would you do the query SELECT * FROM AnnouncementsEntry WHERE entryId NOT IN (SELECT entryId FROM AnnouncementsFlag WHERE entryId = AnnouncementsEntry.entryId and value=-1) in Dynamic SQL Query?

I was having trouble replicate the condition:

entryId = AnnouncementsEntry.entryId

Thx,
Antonio.
thumbnail
Antonio Musarra,修改在12 年前。

RE: Dynamic Query help

Junior Member 帖子: 66 加入日期: 11/8/9 最近的帖子
I solved it this way:

DynamicQuery announcementsEntryDynamicQuery = DynamicQueryFactoryUtil.forClass(AnnouncementsEntry.class, "AnnouncementsEntry");

announcementsEntryDynamicQuery.addOrder(OrderFactoryUtil.desc("type"));
announcementsEntryDynamicQuery.addOrder(OrderFactoryUtil.desc("priority"));
announcementsEntryDynamicQuery.add(RestrictionsFactoryUtil.eq("userId", user.getUserId()));
announcementsEntryDynamicQuery.add(PropertyFactoryUtil.forName("entryId").notIn(
DynamicQueryFactoryUtil.forClass(AnnouncementsFlag.class,"AnnouncementsFlag")
.add(PropertyFactoryUtil.forName("value").eq(flagValue))
.add(PropertyFactoryUtil.forName("userId").eq(user.getUserId()))
.add(PropertyFactoryUtil.forName("AnnouncementsFlag.entryId").eqProperty("AnnouncementsEntry.entryId"))
.setProjection(ProjectionFactoryUtil.property("entryId"))
));

Thx,
Antonio.