带弹簧的EclipseLink - 无法持久保存到Db中

时间:2010-10-29 05:01:10

标签: java spring jpa eclipselink

我使用Spring + EclipseLink 2来管理Derby数据库上的实体。从db中选择对象工作正常,但是当我尝试持久化时,没有任何反应。程序正确执行,不会抛出异常。我可能做错了,因为我对Spring不熟悉,感谢您的意见和建议:)

ServerDaoDb方法:

@Transactional
public void addServer(Server server) {
    EntityManager em = emf.createEntityManager();
    emf.createEntityManager().persist(server);
    em.close();
}

应用程序上下文是:

 ... 
 <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="SpringPratiquePU" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean 
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

Persistence.xml:

  <persistence-unit name="SpringPratiquePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>net.athom.spring.examples.models.eas.Server</class>
    <class>net.athom.spring.examples.models.eas.Node</class>
    <properties>
      <property name="eclipselink.target-database" value="DERBY"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/SpringPratique"/>
      <property name="javax.persistence.jdbc.password" value="clem"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="clem"/>
    </properties>
  </persistence-unit>
</persistence>

调试跟踪:

DEBUG JpaTransactionManager:365 - Creating new transaction with name [net.athom.spring.examples.service.impl.ServerManagerImpl.addServer]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
[EL Info]: 2010-10-29 15:33:27.443--ServerSession(14894886)--EclipseLink, version: Eclipse Persistence Services - 2.0.2.v20100323-r6872
[EL Info]: 2010-10-29 15:33:28.606--ServerSession(14894886)--file:/C:/netbeanProject/SpringPratique/src/_SpringPratiquePU login successful
15:33:28,893       DEBUG JpaTransactionManager:323 - Opened new EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1779885] for JPA transaction
15:33:28,951       DEBUG DefaultListableBeanFactory:242 - Returning cached instance of singleton bean 'transactionManager'
15:33:28,952       DEBUG JpaTransactionManager:286 - Found thread-bound EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1779885] for JPA transaction
15:33:28,953       DEBUG JpaTransactionManager:470 - Participating in existing transaction
15:33:29,266       DEBUG JpaTransactionManager:752 - Initiating transaction commit
15:33:29,267       DEBUG JpaTransactionManager:462 - Committing JPA transaction on EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1779885]
15:33:29,268       DEBUG JpaTransactionManager:548 - Closing JPA EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@1779885] after transaction
15:33:29,308       DEBUG EntityManagerFactoryUtils:328 - Closing JPA EntityManager

3 个答案:

答案 0 :(得分:3)

您的错误在这里:

@Transactional
public void addServer(Server server) {
    EntityManager em = emf.createEntityManager();
    emf.createEntityManager().persist(server);
    em.close();
}

您正在创建两个不同的EntityManager实例,即您要关闭的em,以及在您使用emf.createEntityManager()的下一行创建的新实例,您可以直接使用它来保留更改。

试试这个:

@Transactional
public void addServer(Server server) {
    EntityManager em = emf.createEntityManager();
    em.persist(server);
    em.close();
}

我想当你关闭你的em实例时,你的更改会被写入数据库,但是如果它们不是,你必须在关闭em.flush();实例之前添加em

答案 1 :(得分:3)

我不确定您的配置到底出了什么问题(我猜我手动创建的EntityManager无法参与@Transactional个事务),但Spring中的典型JPA配置如下所示,所以你不需要手动创建EntityManager(还要注意工厂bean的类名):

@PersistenceContext
private EntityManager em;

@Transactional  
public void addServer(Server server) {  
    em.persist(server);  
} 

<bean id="entityManagerFactory"   
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">   
    <property name="persistenceUnitName" value="SpringPratiquePU" />   
</bean>

答案 2 :(得分:3)

感谢您的回答,两者都非常有帮助。我最终使用了LocalContainerEntityManagerFactoryBean,因此我可以将实体管理器注入我的dao。 我在lib文件夹中添加了servlet-agent-2.5.6.jar并传递了VM选项: -javaagent:路径/到/ LIB / ervlet剂-2.5.6.jar

然后我修改了ApplicationContext.xml:

 <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="SpringPratiquePU" />
         <property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
    </bean>

我的道:   ...

  @PersistenceContext
  private EntityManager em;


   @Transactional
   public void addServer(Server server) {
       em.persist(server);
   }

持久性和交易现在就像魅力一样!