EclipseLink JPA:对象不是已知的实体类型

时间:2013-11-18 17:05:55

标签: spring jpa eclipselink

我已经看到很多关于这个问题的问题,但没有适合我的解决方案。 此外,我只有当我尝试持久化对象或更新它时才会收到此错误,当我从数据库中获取它时一切都很好。 这是实体列表:

@Entity
@Table(name="NEWS")
public class News implements Serializable{

@Id 
@SequenceGenerator(name = "news_seq_gen", sequenceName = "news_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "news_seq_gen")

private int newsId;

@Column(name="newsTitle", nullable = false, length = 100)
private String newsTitle;
@Column(name="newsBrief", nullable = false, length = 500)
private String newsBrief;
@Column(name="newsContent", nullable = false, length = 2048)
private String newsContent;
@Column(name="newsDate", nullable = false)
private Date newsDate;
@Transient
private boolean selected=false;

public News(){

}
public News(int id, String title, String brief, String content, Date date)
{
    this.newsBrief = brief;
    this.newsContent= content;
    this.newsTitle= title;
    this.newsDate= date;
    this.newsId=id;
}   
    //getters and setters   
}

spring config:

    <context:annotation-config />
    <tx:annotation-driven />
        <beans:bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <beans:property name="entityManagerFactory" ref="entityManagerFactory" />
    </beans:bean>

    <context:component-scan base-package="com.epam.testapp.daoImpl" />
    <beans:bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location" value="/WEB-INF/config.properties"></beans:property>
    </beans:bean>

    <beans:bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />


    <beans:bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="${DATABASE_DRIVER_NAME}" />
        <beans:property name="url" value="${DATABASE_URL}" />
        <beans:property name="username" value="${DATABASE_USERNAME}" />
        <beans:property name="password" value="${DATABASE_PASS}" />
    </beans:bean>



    <beans:bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="loadTimeWeaver">
            <beans:bean
                class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
        </beans:property>
            <beans:property name="persistenceUnitName" value="nEntityManager" />
        <beans:property name="jpaVendorAdapter">
            <beans:bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <beans:property name="database" value="ORACLE" />
            </beans:bean>
        </beans:property>
        <beans:property name="persistenceXmlLocation" value="WEB-INF/persistence.xml" />
        <beans:property name="persistenceUnitName" value="nEntityManager" />

    </beans:bean>
</beans:beans>

的persistence.xml

<persistence-unit name="nEntityManager"
        transaction-type="RESOURCE_LOCAL">
        <provider>>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <class>com.epam.testapp.domain.News</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="INFO"/>
            <property name="eclipselink.target-database" value="Oracle" />
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521/XE" />
            <property name="javax.persistence.jdbc.user" value="system" />
            <property name="javax.persistence.jdbc.password" value="1234" />
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
        </properties>
    </persistence-unit>
</persistence>

dao:

@Repository("JpaNewsDaoImpl")
public class JPANewsDaoImpl extends DAO implements NewsDao{

    @PersistenceContext()
    private EntityManager eManager;

    @Transactional(readOnly=true)
    @Override
    public List<News> listNews(boolean isrus) throws DAOTechnicException,
            DAOLogicException {
        System.out.println("in jpa");

        Query query = eManager.createQuery("select T FROM News T",News.class);
        return query.getResultList();
    }

    @Transactional
    @Override
    public void addNews(News oneNews, boolean isrus) throws DAOLogicException,
            DAOTechnicException {
        //eManager.merge(oneNews);
        eManager.persist(oneNews);
        }
         }

例外:

 Servlet.service() for servlet [action] in context with path [/strts-spring-hbnt] threw exception [java.lang.IllegalArgumentException: Object: com.epam.testapp.domain.News@179a8d8 is not a known entity type.] with root cause
java.lang.IllegalArgumentException: Object: com.epam.testapp.domain.News@179a8d8 is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4222)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at com.sun.proxy.$Proxy12.persist(Unknown Source)
    at com.epam.testapp.daoImpl.JPANewsDaoImpl.addNews(JPANewsDaoImpl.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy14.addNews(Unknown Source)
    at com.epam.testapp.service.NewsServiceImpl.addNews(NewsServiceImpl.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy24.addNews(Unknown Source)
    at com.epam.testapp.presentation.action.NewsAction.addSubmit(NewsAction.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.epam.testapp.filter.RequestFilter.doFilter(RequestFilter.java:28)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
请告诉我错误在哪里?

修改 好吧,我试图改变generation.type,某处写有<exclude-unlisted-classes>false</exclude-unlisted-classes>的错误,我删除它没有成功,添加

<beans:property name="jpaPropertyMap">
            <beans:props>
                <beans:prop key="eclipselink.weaving">false</beans:prop>
            </beans:props>
        </beans:property>

我也看到它可能因为两个类加载器而发生,但它似乎不在我的配置中。

编辑2:

此外,我发现这篇文章http://www.future-edge.nl/blog/jpa-classloading-problem/似乎与我的问题有关,我试过这个

ClassLoader loader1 = my.package.MyEntity.class.getClassLoader();
System.out.println("MyEntity's class loader is " + loader1);

Query q = entityManager.createNamedQuery("MyEntity.findAll");
List entityObjects = q.getResultList();

for (Object entityObject: entityObjects) {
  ClassLoader loader2 = entityObject.getClass().getClassLoader();
  System.out.println("Class loader of entity " + entityObject + " is " + loader2);
}

第一个类加载器是org.apache.catalina.loader.StandardClassLoader 第二个org.springframework.instrument.classloading.SimpleInstrumenableClassloader

1 个答案:

答案 0 :(得分:2)

问题出现在classLoader中,我已经添加到tomcat spring-instrument-tomcat-3.1.1.jar的lib目录中,并在context.xml中定义了spring class loader就像这样

<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/> 

并在entityManagerFactory bean中将timeweaver更改为

<beans:property name="loadTimeWeaver">
            <beans:bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </beans:property>