Friday, February 26, 2016

Dynamic Query in Liferay


Today we will discuss about Dynamic Query in Liferay. As we already know Liferay Service Builder provide basic CRUD methods but sometimes we need specific methods with different conditions . For this we have two approaches:-

1)Custom Sql in Liferay.
2)Dynamic Query

The problem with custom query is that we need to build services again and again. But in case of dynamic queries no need to build services.

Note:- 

1)Dynamic Queries is used only to Retrieve Data.
2)If you know Hibernate it is similar to Criteria API.



Now Lets write some basic queries:-

First we decide on which table we need to write query.Their are two cases:-

1)Liferay Generated Table
Liferay automatically generated tables like user_, usergroup etc.
In this case we create dynamic query object as:-



2)Custom Tables
Tables you create for your requirement .In this case we create dynamic query as object :-



The basic difference between two statements is Class loader. For Liferay tables we use PortalClassLoaderUtil and for custom tables we use PortletClassLoaderUtil.

In this blog we will write query on user_ table ie User entity.





1)Select query with LIKE operation


OR


Explanation:-
Here both methods return the same result .The only difference between two is how we create the Criteria.In first we use RestrictionsFactoryUtil and in second we use PropertyFactoryUtil . Both do the same task you can use any one.



2)Select query with IN operation



Similarly you can try other operation like equal , between ,greater than, less than etc.

In the next blog we will discuss another important topic Projection in Dynamic query.


Hope this will Help....

Related Post:-

No comments:

Post a Comment

Total Pageviews

Like Us on Facebook

Number Of Unique Visitor

Free counters!