Liferay Dynamic Query API
-Liferay Dynamic Query API is an elegant feature in Liferay portlet development.
-Dynamic Query API can only perform read operations against database. These we will use specially to provide search operations against database.
-Dynamic Query API is independent feature and we can implement anywhere in the development like we can implement in JSP pages, Portlet Action Classes and service implementation classes.
Types of Class Loader-:
Here we have two class loaders
1. Portal Class Loader-:
If any class or model class in portal level then we need to use Portal Class Loader in Dynamic Query Object.
ClassLoader portalClassLoader=PortalClassLoaderUtil.getClassLoader();
We can implement dynamic query against portal level class such as User, Group, Role, Organization, Layout and Layoutset
2. Portlet Classs Loader-:
Portlet Class Loader is related to each portlet.
ClassLoader portletClassLoader=PortletClassLoaderUtil.getClassLoader();
Create Criteria and Add criteria to Dynamic Query Object-:
Criteria represent the reading policies against table or database. We already know when we write the SQL query we will use different operators and condition.
To perform Criteria we use these two utility classes
1. RestrictionsFactoryUtil
2. PropertyFactoryUtil
PropertyFactoryUtil contained all possible operators against columns and PropertyFactoryUtil contains all methods which represent the columns related operators like =,>, <, in and like
Creating Dynamic Query-:
Step1-:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ExpandoValue.class, PortalClassLoaderUtil.getClassLoader());
Step 2-:
DynamicQuery innerQuery = DynamicQueryFactoryUtil.forClass(ExpandoValue.class, PortalClassLoaderUtil.getClassLoader());
Step 3-:
innerQuery.setProjection(ProjectionFactoryUtil.getProjectionFactory().property("classPK"));
//Projection is used to reproduce exact projected values
Step 4-:
Criterion criterion = null;
criterion = RestrictionsFactoryUtil.like("data", "MyData");
criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("data", "Country"));
criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("data", "State"));
Step 5-:
//adding criterion to DynamicQuery Object
innerQuery.add(criterion);
Step 6-:
dynamicQuery.add(PropertyFactoryUtil.forName("classPK").notIn(innerQuery));
dynamicQuery.add(PropertyFactoryUtil.forName("data").eq(String.valueOf(myContactId)));
Multiple methods of Projection, Restriction, Property Interfaces-:

-Liferay Dynamic Query API is an elegant feature in Liferay portlet development.
-Dynamic Query API can only perform read operations against database. These we will use specially to provide search operations against database.
-Dynamic Query API is independent feature and we can implement anywhere in the development like we can implement in JSP pages, Portlet Action Classes and service implementation classes.
Types of Class Loader-:
Here we have two class loaders
1. Portal Class Loader-:
If any class or model class in portal level then we need to use Portal Class Loader in Dynamic Query Object.
ClassLoader portalClassLoader=PortalClassLoaderUtil.getClassLoader();
We can implement dynamic query against portal level class such as User, Group, Role, Organization, Layout and Layoutset
2. Portlet Classs Loader-:
Portlet Class Loader is related to each portlet.
ClassLoader portletClassLoader=PortletClassLoaderUtil.getClassLoader();
Create Criteria and Add criteria to Dynamic Query Object-:
Criteria represent the reading policies against table or database. We already know when we write the SQL query we will use different operators and condition.
To perform Criteria we use these two utility classes
1. RestrictionsFactoryUtil
RestrictionsFactoryUtil is Util class contains WHERE condition operators like (AND, OR, IN).When we want apply WHERE condition related operators then we will RestrictionsFactoryUtil
2. PropertyFactoryUtil
PropertyFactoryUtil contained all possible operators against columns and PropertyFactoryUtil contains all methods which represent the columns related operators like =,>, <, in and like
Creating Dynamic Query-:
Step1-:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ExpandoValue.class, PortalClassLoaderUtil.getClassLoader());
Step 2-:
DynamicQuery innerQuery = DynamicQueryFactoryUtil.forClass(ExpandoValue.class, PortalClassLoaderUtil.getClassLoader());
Step 3-:
innerQuery.setProjection(ProjectionFactoryUtil.getProjectionFactory().property("classPK"));
//Projection is used to reproduce exact projected values
Step 4-:
Criterion criterion = null;
criterion = RestrictionsFactoryUtil.like("data", "MyData");
criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("data", "Country"));
criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("data", "State"));
Step 5-:
//adding criterion to DynamicQuery Object
innerQuery.add(criterion);
Step 6-:
dynamicQuery.add(PropertyFactoryUtil.forName("classPK").notIn(innerQuery));
dynamicQuery.add(PropertyFactoryUtil.forName("data").eq(String.valueOf(myContactId)));
Multiple methods of Projection, Restriction, Property Interfaces-:

Comments
Post a Comment