带有Spring的EclipseLink-无法持久到Db中 [英] EclipseLink with Spring - Can't persist into Db
问题描述
我正在使用Spring + EclipseLink 2来管理Derby数据库上的实体.从数据库中选择对象工作正常,但是当我尝试保留一个对象时,什么也没发生.程序正确执行,并且不会引发异常.由于我对Spring不熟悉,我可能做错了,感谢您的评论和建议:)
I am using Spring + EclipseLink 2 to manage entity on a Derby database. Select object from db works fine but when I try to persist one, nothing happens. Program executes correctly and no exception are thrown. I probably did something wrong, as I'm not familiar with Spring, thanks for your comments and suggestions :)
ServerDaoDb方法:
The ServerDaoDb method :
@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.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
推荐答案
您的错误在这里:
@Transactional
public void addServer(Server server) {
EntityManager em = emf.createEntityManager();
emf.createEntityManager().persist(server);
em.close();
}
您正在创建两个不同的EntityManager实例,您正在关闭的em
,以及在下一行与emf.createEntityManager()
的新实例,您将直接使用它们来保存更改.
Your are creating two different EntityManager instances, the em
, which you are closing, and a new one at the next line with emf.createEntityManager()
which you are using directly to persist your changes.
尝试一下:
@Transactional
public void addServer(Server server) {
EntityManager em = emf.createEntityManager();
em.persist(server);
em.close();
}
我猜想,当您关闭em实例时,所做的更改将写入数据库,但是,如果没有更改,则必须在关闭em
实例之前添加em.flush();
.
I guess that when you close your em instance, your changes are written to the DB, but, in case that they are not, you have to add em.flush();
right before closing the em
instance.
这篇关于带有Spring的EclipseLink-无法持久到Db中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!