Fórumok

DynamicQuery in a custom service

Esteban Lopez, módosítva 13 év-val korábban

DynamicQuery in a custom service

Junior Member Bejegyzések: 26 Csatlakozás dátuma: 2010.11.03. Legújabb bejegyzések
Hi, I have created a service with the service builder. I have put it in a portlet plugin and I have moved the service.jar to the tomcat's lib. My problem is whe I try yo use the DynamicQuery of my service.

I tried to use it with this code

DynamicQuery query = DynamicQueryFactoryUtil.forClass(custom_service.class,getClass().getClassLoader());
query.add(PropertyFactoryUtil.forName("entryId").eq(entryId));
List<custom_assetent_assetcatlocalserviceutil> results = custom_assetEnt_assetCatLocalServiceUtil.dynamicQuery(query);
</custom_assetent_assetcatlocalserviceutil>


But it throws a NullPointerException because Liferay doesn't find the class


09:32:44,465 INFO  [PortletHotDeployListener:369] 1 portlet for sugerencias-portlet is available for use
09:33:03,215 ERROR [DynamicQueryFactoryImpl:83] Unable find model enttod.model.impl.custom_serviceImpl
java.lang.ClassNotFoundException: enttod.model.impl.custom_serviceImpl
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
        at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.getImplClass(DynamicQueryFactoryImpl.java:78)
        at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.getImplClass(DynamicQueryFactoryImpl.java:59)
        at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.forClass(DynamicQueryFactoryImpl.java:33)
        at com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil.forClass(DynamicQueryFactoryUtil.java:23)
        at es.sadiel.entretodos.Sugerencias.processAction(Sugerencias.java:107)
        at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:70)
        at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
...


Can anybody help me?
J J G, módosítva 13 év-val korábban

RE: DynamicQuery in a custom service

New Member Bejegyzések: 16 Csatlakozás dátuma: 2007.06.08. Legújabb bejegyzések
1) There is no need to move your portlet plugin's service jar to tomcat's global lib directory i.e. <TOMCAT_HOME>/lib or <TOMCAT_HOME>/lib/ext.

2) This might sound silly but check in your portlet plugin's <TOMCAT_HOME>\webapps\<YOUR_PLUGIN_PORTLET_CONTEXT_NAME>\WEB-INF\classes folder is your class "enttod.model.impl.custom_serviceImpl.class" available there? This can be the only reason the classloader is unable to find your plugin-specific class assuming enttod.model.impl.custom_serviceImpl implements custom_service as mentioned in your code snippet.

DynamicQuery query = DynamicQueryFactoryUtil.forClass(custom_service.class,getClass().getClassLoader());


3) There is no need to pass the class loader as the second argument, in case your code is not utilizing the classes available outside your portlet context.To be more specific the classes available in portal-impl.jar (available in Liferay tomcat's ROOT context).

For example:-

// This requires PortalClassLoader to be passed.
ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
DynamicQuery dq0 = DynamicQueryFactoryUtil.forClass(User.class, "user", portalClassLoader)
.setProjection(ProjectionFactoryUtil.property("userId"));

// Not required to pass the PortalClassLoader
DynamicQuery dq1 = DynamicQueryFactoryUtil.forClass(CourseType.class, "courseType");


Hope this helps.

Thanks.
Nicholas Tenczar, módosítva 13 év-val korábban

RE: DynamicQuery in a custom service

Junior Member Bejegyzések: 53 Csatlakozás dátuma: 2010.07.15. Legújabb bejegyzések
You can also experience this error if your custom-service.jar is in both the tomcat global lib and your portlet's webapp lib. If you need to access this service from multiple plugins then you can put the the *-service.jar file in the tomcat global lib, but make sure that it is not in any of those plugins' libs. If you do not need to access this service from multiple plugins then you don't need to put the *-service.jar in the global lib at all.
thumbnail
Daniel Breitner, módosítva 13 év-val korábban

RE: DynamicQuery in a custom service

Regular Member Bejegyzések: 105 Csatlakozás dátuma: 2008.07.16. Legújabb bejegyzések
I have a similar error:

Portlet 1 defines a Service and Portlet 2 wants to access that Service with a DynamicQuery.

In this case I can`t find a classloader helping me.

Portalclassloader doesn´t work, the classloader of my current portlet doesn´t work either.


Is there a solution for that ?









-------------------
http://liferay-blogging.blogspot.com
Nicholas Tenczar, módosítva 13 év-val korábban

RE: DynamicQuery in a custom service

Junior Member Bejegyzések: 53 Csatlakozás dátuma: 2010.07.15. Legújabb bejegyzések
If your portlets are in separate plugins then the *-service.jar needs to either be in the lib of both plugins or exclusively in the global lib.

You can get around this issue by declaring multiple portlets in the same plugin. This way you won't need to maintain the same *-service.jar across multiple plugins, or you won't need to manually add the *-service.jar to the global lib.

If these portlets are coupled by their use of this service it would make sense to keep them packaged together. As an added benefit if these portlets both make use of a web framework such as Struts or Spring, you can reduce the amount of memory used when these plugins are loaded. Each plugin using Struts or Spring will load and maintain their own copies of the classes in these frameworks, which can lead to memory issues when many plugins use these frameworks.
thumbnail
Kim Anna Kunc, módosítva 10 év-val korábban

RE: DynamicQuery in a custom service

Junior Member Bejegyzések: 37 Csatlakozás dátuma: 2009.02.18. Legújabb bejegyzések
For me, using two custom portlets (with maven service builder archetype) in separate wars, the following works in Liferay 6.1 GA 2:

ClassLoader cl = Event.class.getClassLoader();
DynamicQuery query = DynamicQueryFactoryUtil.forClass(Event.class, cl);
...

where Event.class is the model class for my entity.
thumbnail
omid ahmadi, módosítva 9 év-val korábban

RE: DynamicQuery in a custom service

Junior Member Bejegyzések: 37 Csatlakozás dátuma: 2013.08.27. Legújabb bejegyzések
Hi friends,
i had same problem and find a solution for it .

instead of calling XfinderUtil directly from second portlet , call it in XLocalServiceImpl.java file method and then use XLocalServiceUtil method in second portlet.

Hope to help youemoticon
thumbnail
Rahul Pande, módosítva 9 év-val korábban

RE: DynamicQuery in a custom service

Expert Bejegyzések: 310 Csatlakozás dátuma: 2010.07.07. Legújabb bejegyzések
Hi Esteban,

As you are writing dynamic query in portlet plugin, you can not use portal class loader object to create the query. You have to use portlet class loader. Please use below code to get the portlet class loader object

ClassLoader portletClassLoader = PortletClassLoaderUtil.getClassLoader();

Use this class loader object to create your dynamic query.


HTH
Rahul Pande