Krutik Jayswal Krutik Jayswal - 4 years ago 100
Java Question

Liferay Many-to-Many RelationShip Throws Class Cast Exception

I am creating one example which demonstrates relationship in liferay.I am taking example of Book entity and Author for many to many relationship.I am following below blog for creating this example.

http://www.liferaysavvy.com/2014/01/liferay-service-builder-many-to-many_12.html

But I faced the issue of class cast exception.In blog also it has been written that it is throwing class cast exception.

11:05:00,422 ERROR [RuntimePageImpl-2][render_portlet_jsp:132] null
java.lang.ClassCastException: com.ds.portlet.library.model.impl.AuthorImpl cannot be cast to com.ds.portlet.library.model.Author
at com.ds.portlet.library.service.persistence.AuthorPersistenceImpl.fetchByPrimaryKey(AuthorPersistenceImpl.java:691)
at com.ds.portlet.library.service.persistence.AuthorPersistenceImpl.findByPrimaryKey(AuthorPersistenceImpl.java:653)
at com.ds.portlet.library.service.persistence.AuthorPersistenceImpl.findByPrimaryKey(AuthorPersistenceImpl.java:59)
at com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice.invoke(ShardPersistenceAdvice.java:54)
at com.liferay.portal.service.persistence.impl.TableMapperImpl.getBaseModels(TableMapperImpl.java:400)
at com.liferay.portal.service.persistence.impl.TableMapperImpl.getRightBaseModels(TableMapperImpl.java:265)
at com.ds.portlet.library.service.persistence.BookPersistenceImpl.getAuthors(BookPersistenceImpl.java:1183)
at com.ds.portlet.library.service.persistence.BookPersistenceImpl.getAuthors(BookPersistenceImpl.java:1162)
at com.ds.portlet.library.service.persistence.BookPersistenceImpl.getAuthors(BookPersistenceImpl.java:1143)
at com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice.invoke(ShardPersistenceAdvice.java:54)
at com.ds.portlet.library.service.base.AuthorLocalServiceBaseImpl.getBookAuthors(AuthorLocalServiceBaseImpl.java:374)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:115)
at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:62)
at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:51)
at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:111)
at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:175)
at com.ds.portlet.library.service.AuthorLocalServiceUtil.getBookAuthors(AuthorLocalServiceUtil.java:321)
at org.apache.jsp.view_jsp._jspService(view_jsp.java:420)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


I would like to know the reason behind this.

Answer Source

I'd expect the involved classes to be twice on the classpath - similar to what David Nebinger answered in this post. Reasons are what he mentions: You might have your xxx-service.jar on the global classpath as well as in your WEB-INF/lib, or you might have it on WEB-INF/lib and added WEB-INF/service as source folder, so that the same classes are in WEB-INF/classes again.

Whenever I've seen classloader issues that a class cannot be typecasted to its legitimate superclass or interface, this was an issue of duplicate classes on the classpath. In the ancient past, this also included the ridiculous message that a java object is not a decendant of java.lang.Object - clearly wrong, but it meant the other java.lang.Object that was on the classpath.

The tricky thing is that they need some specific order of instantiation that is not always easy to willingly reproduce. If you found a situation in which you'll always get the message, it will probably always fail the same way. Many-to-many-relationships in Liferay's service-builder might be a way to trigger these problems.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download