Message Boards

DynamicQuery and Liferay 7.1 OSGI service modules

Kevin Neibarger, modified 5 Years ago.

DynamicQuery and Liferay 7.1 OSGI service modules

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
With the new implementation of OSGI modules in the service layer for Liferay 7.1, is there a way to use the DynamicQuery API without creating a FinderImpl? In 6.2 I could just call the DynamicQuery API from the XxxLocalServiceImpl.java code​​​​​​​
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyClass.class, this.getClassLoader());

dynamicQuery.add(junction).addOrder(OrderFactoryUtil.desc("createDate"));

List<myclass> rosterReviews = this.dynamicQuery(dynamicQuery);</myclass>

In 7.1 the above code simply returns an empty List. Whereas a call to XxxLocalServiceUtil.findByxXX() returns the actual data. I would prefer not having to create a whole other class for DynamicQuery calls.. 
thumbnail
David H Nebinger, modified 5 Years ago.

RE: DynamicQuery and Liferay 7.1 OSGI service modules

Liferay Legend Posts: 14916 Join Date: 9/2/06 Recent Posts
Try using the following:

​​​​​​​DynamicQuery dynamicQuery = myClassLocalService.dynamicQuery();
​​​​​​​The static utils should not be used and honestly the "this.getClassLoader()" thing is too dependent upon the context it is invoked in to ensure the right class loader gets used.

The dynamicQuery() method, available on all Service Builder local services, returns a DQ object ready to deal with the queries against the model interface defined for the local service. It takes all of the guesswork out of the construction and eliminates the class loader context issues faced by code in different modules.
​​​​​​​
Kevin Neibarger, modified 5 Years ago.

RE: DynamicQuery and Liferay 7.1 OSGI service modules

Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
Thanks! Great information, would be nice if this kind of information was in the documentation. Would be nice to see something like that in the JavaDoc. 
thumbnail
David H Nebinger, modified 5 Years ago.

RE: DynamicQuery and Liferay 7.1 OSGI service modules

Liferay Legend Posts: 14916 Join Date: 9/2/06 Recent Posts
You're preaching to the choir, brother.