在 Spring 中使用容器管理(Glassfish)JPA 时事务不更新数据库
如何让 JPA(通过 Hibernate)/Spring 和我的容器管理连接(通过 Glassfish 3.1)发挥良好作用?
更新:不知何故,我的 JPA 连接似乎被缓冲了。如果我修改一条记录,将其字符串转换为大写,那么如果我查找它,我会得到一条包含大写字符串的记录。但底层数据库并没有反映这一点。另一个例子,如果我删除一个实体,然后尝试再次删除它,我会得到一个异常“java.lang.IllegalArgumentException:org.hibernate.ObjectDeletedException:已删除的实例传递给合并:[db.co05in.Test#]”,但底层又是数据库未更新。
包括:
- 的 Spring 注入的入口代码
- 显示 TestService TestServiceImpl
- applicationContext.xml
- persistance.xml
已解决:我不知道为什么,但问题是由名称接线引起的?如果您遇到类似问题,无论如何请参阅“服务实现代码”下的评论。
入口代码:
package com.aerose.partgroupmaster.action;
import com.aerose.mz.db.service.inventory.TestService;
public class DeleteTest{
public com.aerose.mz.db.co05in.Test test; //the POJO JPA DAO
public int id;
private TestService testService;
//GOOD: spring is able to inject the service
public void setTestService(TestService testService){
this.testService = testService;
}
@Override
public String execute() {
test = testService.retreiveTest(id);
testService.deleteTest(test);
return SUCCESS;
}
}
服务实现代码:
package com.aerose.mz.db.service.inventory;
import db.co05in.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
public class TestImpl implements TestService{
@PersistenceContext //needed to add this
private EntityManager em;
//removed this... and edited applicationContext.xml to remove the wiring
//public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory){
// this.em = entityManagerFactory.createEntityManager();
//}
@Transactional
@Override
public void createTest(Test test) {
em.persist(test);
}
@Transactional
@Override
public Test retreiveTest(int id) {
Query query = em.createNamedQuery("Test.findByIdTEST");
query.setParameter("idTEST", id);
return (Test) query.getResultList().get(0);
}
@Transactional
@Override
public Test updateTest(Test test) {
Test test2 = em.merge(test);
return test2;
}
@Transactional
@Override
public void deleteTest(Test test) {
em.remove(em.merge(test));
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-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/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.aerose" />
<tx:annotation-driven />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MySQLLocalDataSource" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="co05in" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<bean id="testService" class="com.aerose.mz.db.service.inventory.TestImpl">
<!-- following not needed -->
<!--<property name="entityManagerFactory" ref="entityManagerFactory"/>-->
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="co05in" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/MySQLLocalDataSource</jta-data-source>
<class>db.co05in.Test</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
</properties>
</persistence-unit>
</persistence>
How can I get JPA(via Hibernate)/Spring and my Container managed connection (via Glassfish 3.1) to play nice?
Update: Somehow my JPA connection seems to be buffered. If I modify a record such that I convert its strings to upper case, then if I look it up I get a record with upper case strings. But the underlying DB does not reflect this. Another example if I delete an entity and then try to delete it again I get an exception "java.lang.IllegalArgumentException: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [db.co05in.Test#]" but again the underlying db is not updated.
Included:
- Entry code showing spring injection of TestService
- TestServiceImpl
- applicationContext.xml
- persistance.xml
Solved: I have no idea why but the issue was somehow caused by wiring by name? Anyways see the comments under "Service implementation code" if you experience a similar issue.
Entry code:
package com.aerose.partgroupmaster.action;
import com.aerose.mz.db.service.inventory.TestService;
public class DeleteTest{
public com.aerose.mz.db.co05in.Test test; //the POJO JPA DAO
public int id;
private TestService testService;
//GOOD: spring is able to inject the service
public void setTestService(TestService testService){
this.testService = testService;
}
@Override
public String execute() {
test = testService.retreiveTest(id);
testService.deleteTest(test);
return SUCCESS;
}
}
Service implementation code:
package com.aerose.mz.db.service.inventory;
import db.co05in.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
public class TestImpl implements TestService{
@PersistenceContext //needed to add this
private EntityManager em;
//removed this... and edited applicationContext.xml to remove the wiring
//public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory){
// this.em = entityManagerFactory.createEntityManager();
//}
@Transactional
@Override
public void createTest(Test test) {
em.persist(test);
}
@Transactional
@Override
public Test retreiveTest(int id) {
Query query = em.createNamedQuery("Test.findByIdTEST");
query.setParameter("idTEST", id);
return (Test) query.getResultList().get(0);
}
@Transactional
@Override
public Test updateTest(Test test) {
Test test2 = em.merge(test);
return test2;
}
@Transactional
@Override
public void deleteTest(Test test) {
em.remove(em.merge(test));
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-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/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.aerose" />
<tx:annotation-driven />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MySQLLocalDataSource" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="co05in" />
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<bean id="testService" class="com.aerose.mz.db.service.inventory.TestImpl">
<!-- following not needed -->
<!--<property name="entityManagerFactory" ref="entityManagerFactory"/>-->
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="co05in" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/MySQLLocalDataSource</jta-data-source>
<class>db.co05in.Test</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
</properties>
</persistence-unit>
</persistence>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用容器管理的事务时,您需要使用 JtaTransactionManager:
据我了解,在您的情况下它应该可以正常工作,因为 Hibernate 已经配置为通过其属性使用 JTA 事务。
When working with container-managed transactions you need to use
JtaTransactionManager
:As far as I understand in your case it should work fine since Hibernate is already configured to use JTA transactions via its properties.