Fórum

DynamicQuery in a custom service

Esteban Lopez, modificado 13 Anos atrás.

DynamicQuery in a custom service

Junior Member Postagens: 26 Data de Entrada: 03/11/10 Postagens Recentes
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, modificado 13 Anos atrás.

RE: DynamicQuery in a custom service

New Member Postagens: 16 Data de Entrada: 08/06/07 Postagens Recentes
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, modificado 13 Anos atrás.

RE: DynamicQuery in a custom service

Junior Member Postagens: 53 Data de Entrada: 15/07/10 Postagens Recentes
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, modificado 13 Anos atrás.

RE: DynamicQuery in a custom service

Regular Member Postagens: 105 Data de Entrada: 16/07/08 Postagens Recentes
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, modificado 13 Anos atrás.

RE: DynamicQuery in a custom service

Junior Member Postagens: 53 Data de Entrada: 15/07/10 Postagens Recentes
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, modificado 10 Anos atrás.

RE: DynamicQuery in a custom service

Junior Member Postagens: 37 Data de Entrada: 18/02/09 Postagens Recentes
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, modificado 9 Anos atrás.

RE: DynamicQuery in a custom service

Junior Member Postagens: 37 Data de Entrada: 27/08/13 Postagens Recentes
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, modificado 9 Anos atrás.

RE: DynamicQuery in a custom service

Expert Postagens: 310 Data de Entrada: 07/07/10 Postagens Recentes
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