Introduction of Dynamic query:-
Liferay provides several ways by which we can retrieve data from database. One of them is dynamic query. You can easily fire complex query using dynamic query and it will reduce overhead of creating custom finder methods
How to build Dynamic Query:
Example like :
Dealing with Liferay's built in entities like Users, Organizations, Assets, AssetCategories
As DynamicQuery API looks for current classloader, for access, but as you are dealing with Portal level
class [User.class], you have to specify portalClassLoader as below.
ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
DynamicQuery query =DynamicQueryFactoryUtil.forClass(User.class, portalClassLoader);
-
Note 2: Dealing with custom entity within portlet.
Liferay provides several ways by which we can retrieve data from database. One of them is dynamic query. You can easily fire complex query using dynamic query and it will reduce overhead of creating custom finder methods
How to build Dynamic Query:
DynamicQuery dynamicQuery= DynamicQueryFactoryUtil.forClass(Entity_Name.class, PortalClassLoaderUtil.getClassLoader());
OR
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Entity_Name.class, PortletClassLoaderUtil.getClassLoader());
Note 1: If you working with dynamic query inside your custom portlet and trying to access liferay core entity (i.e, database) then use Portal Class Loader.
Dealing with Liferay's built in entities like Users, Organizations, Assets, AssetCategories
As DynamicQuery API looks for current classloader, for access, but as you are dealing with Portal level
class [User.class], you have to specify portalClassLoader as below.
ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
DynamicQuery query =DynamicQueryFactoryUtil.forClass(User.class, portalClassLoader);
-
Note 2: Dealing with custom entity within portlet.
DynamicQuery dynamicQuery=DynamicQueryFactoryUtil.forClass(Application.class);
example1:-
1.SELECT * FROM User_ WHERE lastName='Bloggs'
Dynamic Query:
DynamicQuer dynamicQuery = DynamicQueryFactoryUtil.forClass(User.class, PortalClassLoaderUtil.getClassLoader());
dynamicQuery.add(PropertyFactoryUtil.forName("lastName").eq("Bloggs"));
List<User> userList = UserLocalServiceUtil.dynamicQuery(dynamicQuery);
Example2:-
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);
| ||||