Saturday, September 23, 2017

Liferay Error : Unable to find model in Dynamic Query


We already know how to write dynamic query in Liferay. If we create Dynamic Query object in same portlet in which our service are then we can use :-


DynamicQuery query = DynamicQueryFactoryUtil.forClass(Employee.class, "emp",PortletClassLoaderUtil.getClassLoader());
view raw Problem.java hosted with ❤ by GitHub


But if we use this code in another portlet ie Services in one portlet and our portlet is different then we get error on our console:-


[DynamicQueryFactoryImpl:96] Unable find model com.liferay.sample.model.impl.EmployeeImpl
java.lang.ClassNotFoundException: com.liferay.sample.model.impl.EmployeeImpl

Solution:-
In this case you can create Dynamic Query Object as:-

ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Employee.class, "emp", classLoader);
view raw Solution.java hosted with ❤ by GitHub

Explanation:-
The only difference is the way how we use the Class Loader.

No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!