Liferay多对多关系引发类强制转换异常[英] Liferay Many-to-Many RelationShip Throws Class Cast Exception

问题描述

我正在创建一个示例来演示 liferay 中的关系.我正在以 Book 实体和作者的多对多关系为例.我正在关注下面的博客来创建这个示例.

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

但是我遇到了类转换异常的问题.在博客中也写过它正在抛出类转换异常.

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)

我想知道这背后的原因.

推荐答案

我希望所涉及的类在类路径上出现两次 - 类似于 David Nebinger 在 这篇文章.原因是他提到的:您可能在全局类路径和 WEB-INF/lib 中都有您的 xxx-service.jar,或者您可能在 WEB-INF/lib 上有它并添加了 WEB-INF/service 作为源文件夹,因此相同的类是再次在 WEB-INF/classes 中.

每当我看到类加载器问题,即无法将类类型转换为其合法的超类或接口时,这都是类路径上重复类的问题.在过去,这还包括一个荒谬的消息,即 java 对象不是 java.lang.Object 的后代 - 显然是错误的,但它意味着 另一个 java.lang.Object 在类路径.

棘手的是,他们需要一些特定的实例化顺序,而这种顺序并不总是很容易随意复制.如果您发现总是会收到消息的情况,那么它可能总是以同样的方式失败.Liferay 的服务构建器中的多对多关系可能是触发这些问题的一种方式.

本文地址:https://www.itbaoku.cn/post/978487.html