Announcement Announcement Module
Collapse
No announcement yet.
Problems with EclipseLinkJpaVendorAdapter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with EclipseLinkJpaVendorAdapter

    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)
    Code:
    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;
        }
    
    }
    JpaUserDao.java
    Code:
    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);
    
        }
    }
    persistence.xml
    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>
    context.xml (the bean context)
    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>
    TestPersistence.java (the test class :-) )
    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 start the class TestPersistence, I get the following exception:

    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
    When I look to the database, the table will be applied in the correct way.
    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

  • #2
    Do you use 3.1.1?

    I have a similar problem dith eclipselink after upgrading from 3.1.0 to 3.1.1. The configuration is working, because after rollback to 3.1.0 all works without configuration changes.

    I think the problem in version 3.1.1.

    Comment


    • #3
      Originally posted by ilyaskorik View Post
      I have a similar problem dith eclipselink after upgrading from 3.1.0 to 3.1.1. The configuration is working, because after rollback to 3.1.0 all works without configuration changes.

      I think the problem in version 3.1.1.
      I am actually using 3.1.0 RELEASE I got it working with hibernate but would like to use eclipselink(my company wants that). Would you like to share your jpaApplicationContext.xml, persistence.xml, a sample entity bean and a sample dao so I can compare it with my configuration. Would be very helpfull.

      Comment


      • #4
        I'm also struggling with this... Differently from you, I'm trying to use <jpa:repositories> to initiate my repositories that implement JpaRepository...

        I tried your approach and I get the same result... All I found on the web is the use of <class> on persistence.xml but it makes no difference as far as I can tell...

        The same code works perfectly if I just switch to another persistence-unit I have configured using Hibernate...

        Comment

        Working...
        X