如何获得JPA(通过Hibernate)/ Spring和我的Container管理连接(通过Glassfish 3.1)才能发挥出色?
更新:某种程度上我的JPA连接似乎是缓冲的。如果我修改一个记录,以便我将其字符串转换为大写,那么如果我查找它,我会得到一个大写字符串的记录。但底层数据库并未反映出这一点。另一个例子,如果我删除一个实体,然后尝试再次删除它我得到一个异常“java.lang.IllegalArgumentException:org.hibernate.ObjectDeletedException:已删除的实例传递给合并:[db.co05in.Test#]”但再次是底层db未更新。
包括:
解决了:我不知道为什么但这个问题是由某个名称的布线引起的?如果遇到类似的问题,无论如何都要看“服务实施代码”下的评论。
输入代码:
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>
答案 0 :(得分:1)
使用容器管理的事务时,您需要使用JtaTransactionManager
:
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
据我所知,在你的情况下它应该可以正常工作,因为Hibernate已经配置为通过其属性使用JTA事务。