与Spring e Jpa的交易

时间:2011-07-12 13:28:23

标签: hibernate spring jpa transactions

我有same problem,我的交易没有写入数据库。我认为这个Spring问题不是框架的错误,而是配置文件的问题(applicationContext.xml用于理解)所以我把我的配置文件:

...    
            <!-- this is a bridge for entityManager and PersistenceUnit -->
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
            <property name="persistenceUnitName" value="fb-persistence" />
        </bean>


    <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
        <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean name="pluginDaoImpl" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" />
</beans>

界面是:

public interface PluginDao {

    public Plugin load (int id);

    public void save(Plugin plg);
}

接口的实现是:

public class PluginDaoImpl implements PluginDao {

    @PersistenceContext (unitName="fb-persistence")
    private EntityManager em;

    public void setEntityManager(EntityManager entityManager) {
        this.em = entityManager;
    }

    @Transactional
    public Plugin load(int id) {
        return em.find(Plugin.class, id);

    }
    @Transactional(readOnly=false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void save(Plugin plg) {
        em.persist(plg);
        //em.flush();

    }
}

和dulcis in fundo,我的PluginTest(用于脏测试)是:

public class PluginTest{

    public static void main(String[] args) {        
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PluginDao dao = (PluginDao) applicationContext.getBean("pluginDaoImpl");

        Plugin plugin1 = new Plugin();
        //inserisco un nuovo plugin
        //plugin.setId(14);
        plugin1.setMethod("prova");
        plugin1.setDescrizione("questa Bean Class!");
        dao.save(plugin1);

        Plugin plugin = new Plugin();
        //carico un plugin per id
        plugin =  dao.load(9);

        System.out.println("id: " + plugin.getId() + 
                         " Descrizione: " + plugin.getDescrizione() + 
                         " Method: " + plugin.getMethod());
    }

如果我现在添加方法PluginDaoImpl.save(),em.flush行代码我收到此错误:

Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress *

*原因没有具体说明。

1 个答案:

答案 0 :(得分:1)

您遗失<tx:annotation-driven transaction-manager="myTransactionManager"/>
在您的context.xml中

(见Using @Transactional

像这样添加tx名称空间:

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">