Hi,
I want to use the EclipseLinkJpaVendorAdapter to snap to the database.
Here are my classes and configuration.
PUser.java (the entity I want to persist)
JpaUserDao.javaCode:package de.thmWeb.entities; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class PUser implements Serializable { private Integer idx; private String username; @Id @GeneratedValue public Integer getIdx() { return idx; } public void setIdx(Integer idx) { this.idx = idx; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
persistence.xmlCode:package de.thmWeb.persistence; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import de.thmWeb.entities.PUser; @Repository @Transactional public class JpaUserDao { @PersistenceContext private EntityManager em; public void addUser(PUser user) { em.persist(user); } }
context.xml (the bean context)Code:<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="springtest"> <class>de.thmWeb.entities.PUser</class> <properties> <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost/springtest" /> <property name="eclipselink.jdbc.user" value="springtest" /> <property name="eclipselink.jdbc.password" value="XXXXXXX" /> <property name="eclipselink.jdbc.native-sql" value="true" /> <property name="eclipselink.logging.level" value="SEVERE" /> <property name="eclipselink.ddl-generation" value="create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit> </persistence>
TestPersistence.java (the test class :-) )Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <context:component-scan base-package="de.thmWeb.entities" /> <context:annotation-config /> <tx:annotation-driven /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/springtest" /> <property name="username" value="springtest" /> <property name="password" value="XXXXX" /> </bean> <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceUnitName" value="springtest" /> <property name="jpaDialect" ref="jpaDialect" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform" /> </bean> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" /> </property> </bean> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect " /> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor " /> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <bean id="userDao" class="de.thmWeb.persistence.JpaUserDao" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="emf" /> </bean> </beans>
When I start the class TestPersistence, I get the following exception:Code:package de.thmWeb; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import de.thmWeb.entities.PUser; import de.thmWeb.persistence.JpaUserDao; public class TestPersistence { public static void main(String[] args) { System.out.println("start"); ApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); JpaUserDao userDao = (JpaUserDao) context.getBean("userDao"); final PUser user = new PUser(); user.setUsername("foo"); userDao.addUser(user); System.out.println("ready"); } }
When I look to the database, the table will be applied in the correct way.Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageE xception: Object: de.thmWeb.entities.PUser@4f536eec is not a known entity type.; nested exception is java.lang.IllegalArgumentException: Object: de.thmWeb.entities.PUser@4f536eec is not a known entity type.
at org.springframework.orm.jpa.EntityManagerFactoryUt ils.convertJpaAccessExceptionIfPossible(EntityMana gerFactoryUtils.java:287)
at org.springframework.orm.jpa.DefaultJpaDialect.tran slateExceptionIfPossible(DefaultJpaDialect.java:12 0)
at org.springframework.orm.jpa.AbstractEntityManagerF actoryBean.translateExceptionIfPossible(AbstractEn tityManagerFactoryBean.java:368)
at org.springframework.dao.support.ChainedPersistence ExceptionTranslator.translateExceptionIfPossible(C hainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.tr anslateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:108)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:625)
at de.thmWeb.persistence.JpaUserDao$$EnhancerByCGLIB$ $d443f862.addUser(<generated>)
at de.thmWeb.TestPersistence.main(TestPersistence.jav a:17)
Caused by: java.lang.IllegalArgumentException: Object: de.thmWeb.entities.PUser@4f536eec is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWo rkImpl.registerNewObjectForPersist(UnitOfWorkImpl. java:4147)
at org.eclipse.persistence.internal.jpa.EntityManager Impl.persist(EntityManagerImpl.java:368)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCre ator$SharedEntityManagerInvocationHandler.invoke(S haredEntityManagerCreator.java:240)
at $Proxy8.persist(Unknown Source)
at de.thmWeb.persistence.JpaUserDao.addUser(JpaUserDa o.java:19)
at de.thmWeb.persistence.JpaUserDao$$FastClassByCGLIB $$442008c1.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy. java:191)
at org.springframework.aop.framework.Cglib2AopProxy$C glibMethodInvocation.invokeJoinpoint(Cglib2AopProx y.java:692)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :150)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
... 6 more
If I use the same entity in the old way (Persistence.createEntityManagerFactory("springtes t") ...) without spring, everything works fine.
Can anybody help me with this problem?
Best Regards
Thomas Müller


Reply With Quote
