0

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.

Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38

1 Answers1

0

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.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • I did check it,but its not like that,may be it will be help full to me if you can answer this question....http://stackoverflow.com/questions/30236172/liferay-maven-service-builder-portletdeveloper-studio – Krutik Jayswal May 27 '15 at 10:16
  • @suyash I am still standing behind my comment in this answer: *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* . Check this. Double check. Make extra extra *extra* sure that there are no duplicates. Then, when you find the duplicates, remove them. Problem solved. – Olaf Kock Sep 02 '15 at 07:17
  • I am facing an similar issue , but i am unable to find any duplicate entries, could you please suggest some common places where these could be found and are common in most of the cases?? – Navankur Chauhan Feb 03 '17 at 13:53
  • add `tomcat/lib` and `tomcat/lib/ext` to the list of directories above. And any other directory that you add to the classpath for yourself. While there's no guarantee that this is all, at least those are common places for duplicate classes. – Olaf Kock Feb 03 '17 at 14:58