Dynamic Query in liferay using Or Operator.

Pradip Gadewar, modified 12 Years ago. New Member Posts: 5 Join Date: 11/19/13 Recent Posts
Hello All,

I am new to liferay, I just build the service layer and now I have to retrieving list of records from table. I just want to write Sql Statement
for following Query.

"Select EmpName from Emp
where EmpName ='Pradip' OR EmpName = 'Ravi' OR EmpName ='Roshan' OR EmpName ='Sushil' ";

I have to write DynamicQuery in Liferay for the above Query.
Consider that i have to give OR condition for 8 to 10 Names.


Can Anybody know how to do this??

--
Thanks in Advance
Pradip Gadewar
thumbnail
Ahmad Heba Tul Baseet, modified 12 Years ago. Junior Member Posts: 72 Join Date: 5/12/11 Recent Posts
Hi Pradip,
take a look on
https://www.liferay.com/de/community/forums/-/message_boards/message/1266520

Thanks.
Pradip Gadewar, modified 12 Years ago. New Member Posts: 5 Join Date: 11/19/13 Recent Posts
Hi Ahmad,

Thanks for your Reply, I have checked your link but it will be worked for only 2 names but when i have to compare with 8 to 10 names in or conditions more clearly my DynamicQuery is as follows

DynamicQuery activityQuery=DynamicQueryFactoryUtil.forClass(ActivityData.class,(ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader"))
.add(RestrictionsFactoryUtil.between("publicationliveDate", actdate,curdate))
.add(PropertyFactoryUtil.forName("centretype").eq("LearningCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("SocialCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("CSRCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("CommunicationCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("PolicyCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("Leaderboard"))
.add(PropertyFactoryUtil.forName("centretype").eq("Idea Centre"))
.add(PropertyFactoryUtil.forName("centretype").eq("AppreciationCentre"))
.add(PropertyFactoryUtil.forName("centretype").eq("Public Page"))
.addOrder(OrderFactoryUtil.desc("publicationliveDate"));

I have to select a records from the ActivityData Table whose centretypes are LearningCentre or SocialCentre or CSRCentre or CommunicationCentre or PolicyCentre or Leaderboard or AppreciationCentre or Public Page.

In such case I am not able to use Or operator Please Reply me as early as possible....
thumbnail
Kumar Vivek, modified 12 Years ago. Junior Member Posts: 51 Join Date: 9/5/11 Recent Posts
HI Pradip Gadewar,
As far as i can see your approach towards the dynamic query is wrong . its same as in hibernate .
Here is a sample where you can find your solution
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);
Criterion criterion = null;
criterion = RestrictionsFactoryUtil.like("subject", StringPool.PERCENT + "Test Subject"+ StringPool.PERCENT);
criterion = RestrictionsFactoryUtil.and(criterion, RestrictionsFactoryUtil.between("create_date",10/02/2012,10/03/2012));
criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("status", "Pending"));
dynamicQuery.add(criterion); 


Regards
KV
thumbnail
sushil patidar, modified 12 Years ago. Expert Posts: 467 Join Date: 10/31/11 Recent Posts
Hi,
you can use use disjunction to put OR conditions as follows.
    DynamicQuery activityQuery=DynamicQueryFactoryUtil.forClass(ActivityData.class,    (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader"));
		 
		 Disjunction disjunction = RestrictionsFactoryUtil.disjunction();
		 disjunction.add(PropertyFactoryUtil.forName("propertyName1").eq("value1"));
		 disjunction.add(PropertyFactoryUtil.forName("propertyName2").eq("value2"));
		 activityQuery.add(disjunction);
    

i hope it may help you.

Regards
Pradip Gadewar, modified 12 Years ago. New Member Posts: 5 Join Date: 11/19/13 Recent Posts
Hello All,

Finally i got the answer we can use dynamic query using the OR operator as follows.

String[] centerTypesArray = { "Leaderboard", "LearningCentre", "CSRCentre", "SocialCentre", "CommunicationCentre" , "PolicyCentre" , "Idea Centre" , "AppreciationCentre" ,"Public Page"};

DynamicQuery activityQuery=DynamicQueryFactoryUtil.forClass(ActivityData.class,(ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader"))
.add(RestrictionsFactoryUtil.between("publicationliveDate", actdate,curdate))
.add(PropertyFactoryUtil.forName("centretype").in(centerTypesArray))
.addOrder(OrderFactoryUtil.desc("publicationliveDate"));

when we use such kind of query it will search records from the Leaderboard or CSRCentre or others as specified in centerTypesArray .
I am Thankful to all for replying me.