在Spring中使用容器管理(Glassfish) JPA时,事务不更新数据库



我怎样才能得到JPA(通过Hibernate)/Spring和我的容器管理连接(通过Glassfish 3.1)玩得好?

Update:不知何故,我的JPA连接似乎被缓冲了。如果我修改一条记录,将其字符串转换为大写,那么如果我查找它,我将得到一个大写字符串的记录。但是底层数据库并没有反映这一点。另一个例子,如果我删除一个实体,然后试图再次删除它,我得到一个异常"java.lang。ilegalargumentexception: org.hibernate.ObjectDeletedException:已删除实例传入合并:[db.co05in.]Test#]"但是底层数据库仍然没有更新。

包括:

  • 显示TestService spring注入的入口代码
  • TestServiceImpl
  • persistance.xml

Solved:我不知道为什么,但这个问题是由名称引起的?无论如何,如果你遇到类似的问题,请参阅"服务实现代码"下的评论。

输入代码:

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));
    }
}

<?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>

使用容器管理的事务时,您需要使用JtaTransactionManager:

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager" /> 

据我所知,在您的情况下,它应该工作得很好,因为Hibernate已经配置为通过其属性使用JTA事务。

最新更新