掲示板

Liferay 7 Dynamic Query Error during declaration

7年前 に Zachary Mayry によって更新されました。

Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 14 参加年月日: 17/02/24 最新の投稿
As soon as I declare a Dynamic Query in my portlet, I get an error when loading the page.

Here's the problem line:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Weeks.class, PortletClassLoaderUtil.getClassLoader());

Here's the stack trace when I try loading it:
18:41:39,821 ERROR [http-nio-8080-exec-8][render_portlet_jsp:131] null
java.lang.IllegalStateException: No servlet context name specified
at com.liferay.portal.kernel.portlet.PortletClassLoaderUtil.getServletContextName(PortletClassLoaderUtil.java:53)
at com.liferay.portal.kernel.portlet.PortletClassLoaderUtil.getClassLoader(PortletClassLoaderUtil.java:32)
at com.kaningo.timesheet.portlet.TimesheetPortlet.render(TimesheetPortlet.java:66)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:103)
at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:57)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)
at com.liferay.portlet.ScriptDataPortletFilter.doFilter(ScriptDataPortletFilter.java:57)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:100)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:64)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:105)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$PortletServletWrapper.service(ServletContextHelperRegistrationImpl.java:427)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:50)
at com.liferay.portal.osgi.web.servlet.context.helper.internal.ServletContextHelperRegistrationImpl$RestrictPortletServletRequestFilter.doFilter(ServletContextHelperRegistrationImpl.java:447)
at org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration.doFilter(FilterRegistration.java:121)
at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:45)
at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:70)
at org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:117)
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Zachary Mayry:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Weeks.class, PortletClassLoaderUtil.getClassLoader());


The PortletClassLoaderUtil is a legacy holdover for war-based portlet projects.

If you are using regular OSGi modules, use Weeks.class.getClassLoader() instead since that should get you to the correct class loader.

A better option would be to call WeeksLocalService's dynamicQuery(); method (no arg version) which creates a DQ instance ready for you to populate.

But the best option is to realize that by using DQ outside of your data access layer, you are violating the separation of concerns and exposing too many details about your data access layer to the outside world. Instead, you should add a method to WeeksLocalServiceImpl that the outside world can call and inside of that method you deal with the the DQ usage. This is always the best option because it removes the class loader issue since everything is happing within the same class loader.
7年前 に Zachary Mayry によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 14 参加年月日: 17/02/24 最新の投稿
Thank you for the tips.

I am now facing a new error though however.
Here is the snippet of code in my portlet class.
List<weeks> results = WeeksLocalServiceUtil.findCurrentWeek();</weeks>


And the code in WeeksLocalServiceImpl
import aQute.bnd.annotation.ProviderType;

import java.util.Date;
import java.util.List;

import com.kaningo.timesheet.service.model.Weeks;
import com.kaningo.timesheet.service.service.base.WeeksLocalServiceBaseImpl;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;

/**
 * The implementation of the weeks local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.kaningo.timesheet.service.service.WeeksLocalService} interface.
 *
 * </p><p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see WeeksLocalServiceBaseImpl
 * @see com.kaningo.timesheet.service.service.WeeksLocalServiceUtil
 */
@ProviderType
public class WeeksLocalServiceImpl extends WeeksLocalServiceBaseImpl {
	@Override
	public List<weeks> findCurrentWeek() {
		Date today = new Date();
		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Weeks.class)
				.add(PropertyFactoryUtil.forName("date_start").le(today))
				.add(PropertyFactoryUtil.forName("date_end").ge(today));
		
		return dynamicQuery(query);
	}
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this class directly. Always use {@link com.kaningo.timesheet.service.service.WeeksLocalServiceUtil} to access the weeks local service.
	 */
}</weeks>


And this is the error line when I refresh the page.
22:48:15,878 ERROR [http-nio-8080-exec-5][status_jsp:950] com.liferay.portal.kernel.portlet.PortletContainerException: com.liferay.portal.kernel.portlet.PortletContainerException: javax.servlet.ServletException: java.lang.NoSuchMethodError: com.kaningo.timesheet.service.service.WeeksLocalServiceUtil.findCurrentWeek()Ljava/util/List;

The method is in WeeksLocalServiceUtil
public static java.util.List<com.kaningo.timesheet.service.model.weeks> findCurrentWeek() {
		return getService().findCurrentWeek();
	}</com.kaningo.timesheet.service.model.weeks>
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
That points to some sort of duplicate service out there, i.e. perhaps one was not deployed (even though the code is there, the API module wasn't deployed) or perhaps there's a conflict (i.e. you have a global service jar and a local service jar and the global will always win), ...

It's definitely some sort of environmental issue on your end.
7年前 に Zachary Mayry によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 14 参加年月日: 17/02/24 最新の投稿
So I've setup my entire environment and recopied code.


I'm very new to Liferay. The way I've set up the project is to download the Liferay IDE along with the Liferay 7 bundle.
Make a new workspace project, then make an MVC module along with a Service Builder module.

Add my service.xml code and build the service.
Refresh gradle project, then add my code to WeeksLocalServiceImpl
Refresh gradle project again.

When I call the method WeeksLocalServiceUtil.findCurrentWeek() in my component class it does find it and no longer crashes.
I do get this error now when I refresh the page and I'm not sure what's going on.

18:22:35,265 ERROR [http-nio-8080-exec-9][DynamicQueryFactoryImpl:103] Unable find model com.kaningo.timesheet.service.model.impl.WeeksImpl
java.lang.ClassNotFoundException: com.kaningo.timesheet.service.model.impl.WeeksImpl
7年前 に Zachary Mayry によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 14 参加年月日: 17/02/24 最新の投稿
So I actually solved it myself this time emoticon

Here's the change that fixed it.
DynamicQuery query = DynamicQueryFactoryUtil.forClass(Weeks.class)

was changed to
DynamicQuery query = DynamicQueryFactoryUtil.forClass(WeeksImpl.class)
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
You shouldn't be using or exposing the impl classes, they are never available to the outside world.

The service jar, however, that is meant to be shared.

Also there's another form of the DynamicQueryFactoryUtil.forClass() method, one which accepts a class loader as an argument; when using the DQ outside of your implementation code, you need to include the class loader.

That's another reason to keep your DQ code inside of the implementation, it solves the class loader problem.
7年前 に Zachary Mayry によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 14 参加年月日: 17/02/24 最新の投稿
That's exactly what I did. I wrote all the logic in WeeksLocalServiceImpl class and called it through the corresponding Util class.

Thanks for all the help.
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
yep, that method for implementing has always worked for me.
6年前 に darshan gandhi によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 6 参加年月日: 17/01/03 最新の投稿
Getting issue for Dynamic Query while working with Liferay DXP

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ExchangeRateMaster.class);


If I am giving model class as above, it's showing following error in console.

java.lang.ClassNotFoundException: com.sbi.mst.core.exchange.model.impl.ExchangeRateMasterImpl
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
	at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.getImplClass(DynamicQueryFactoryImpl.java:131)
	at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.getImplClass(DynamicQueryFactoryImpl.java:100)
	at com.liferay.portal.dao.orm.hibernate.DynamicQueryFactoryImpl.forClass(DynamicQueryFactoryImpl.java:42)
	at com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil.forClass(DynamicQueryFactoryUtil.java:25)
	at com.sbi.mst.core.exchange.service.impl.ExchangeRateMasterLocalServiceImpl.getExchnageRateFirstRange(ExchangeRateMasterLocalServiceImpl.java:49)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:163)
	at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:54)
	at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:58)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:56)
	at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:137)
	at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:169)
	at com.sun.proxy.$Proxy861.getExchnageRateFirstRange(Unknown Source)
	at com.sbi.mst.core.exchange.service.ExchangeRateMasterLocalServiceUtil.getExchnageRateFirstRange(ExchangeRateMasterLocalServiceUtil.java:137)
	at com.sbi.exchange.rate.scheduler.ExchangeRateAlertMailJobScheduler.doReceive(ExchangeRateAlertMailJobScheduler.java:58)
	at com.liferay.portal.kernel.messaging.BaseMessageListener.receive(BaseMessageListener.java:26)
	at com.liferay.portal.kernel.scheduler.messaging.SchedulerEventMessageListenerWrapper.receive(SchedulerEventMessageListenerWrapper.java:65)
	at com.liferay.portal.kernel.messaging.InvokerMessageListener.receive(InvokerMessageListener.java:74)
	at com.liferay.portal.kernel.messaging.ParallelDestination$1.run(ParallelDestination.java:52)
	at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask._runTask(ThreadPoolExecutor.java:756)
	at com.liferay.portal.kernel.concurrent.ThreadPoolExecutor$WorkerTask.run(ThreadPoolExecutor.java:667)
	at java.lang.Thread.run(Thread.java:745)


Surprisingly, even it throws above error, code is working fine. Means it fetch correct result.

When I change the model class to modelImpl as below.

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ExchangeRateMasterImpl.class);


This work without any error in console.

Now I am not sure which is the best practice to follow ? Can anyone please suggest ? Is this Liferay DXP issue ? Because from one of the reply in this thread, I come to know, we should use model only and not modelImpl.

Regards
Darshan
thumbnail
6年前 に Jorge Díaz によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration (回答)

Liferay Master 投稿: 753 参加年月日: 14/01/09 最新の投稿
Try replacing DynamicQueryFactoryUtil.forClass({className}.class,cl) with
{className}LocalServiceUtil.dynamicQuery()
That will avoid classloader issues

For examine, in case of JournalArticle, replace DynamicQueryFactoryUtil.forClass(JournalArticle.class, ....) with:
JournalArticleLocalServiceUtil.dynamicQuery()

More info see:
thumbnail
6年前 に Andrew Jardine によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
I was suffering from the same issue. The odd thing is that I used --

ClassLoader classLoader = ClassLoaderUtil.getClassLoader(Calendar.class)


which worked (I mean, no error). I then built my dynamic query and it ran and worked properly, but it still threw the exception in the log. So even though it could not find the CalendarImpl (according to the message) the dynamic query returned the correct results.

This solution though worked and doesn't log the junk.
6年前 に Darshan Gandhi によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 6 参加年月日: 17/01/03 最新の投稿
Hi,
You want get dynamicquery for that try this way

DynamicQuery dynamicquery = Calendar.DynamicQuery();
thumbnail
6年前 に Andrew Jardine によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
There is no such thing.
6年前 に Darshan Gandhi によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

New Member 投稿: 6 参加年月日: 17/01/03 最新の投稿
Hi,
Sorry Try this
DynamicQuery dynamicQuery = CalendarLocalServiceUtil.dynamicQuery();
thumbnail
6年前 に Andrew Jardine によって更新されました。

RE: Liferay 7 Dynamic Query Error during declaration

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
:| ... I know. That is exactly what Jorge said (just a different service) and I was saying it worked for me as well. I was just highlighting that even though the exception is thrown (using the other technique) the query was still working.

For the record, if you are using 7 you should avoid using the *Util classes and instead use the *Service or *LocalService classes.