与 Spring e Jpa 的交易

发布于 2024-11-19 09:47:07 字数 2770 浏览 3 评论 0原文

我有同样的问题,我的事务不写进入数据库。我认为这个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在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 *

*未指定原因。

i have the same problem, my Transactions not write into DB. I think this Spring problem regard is not a bug of framework, but is a problem of configuration file (applicationContext.xml for understand) so I put my configuration file:

...    
            <!-- 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>

a interface is:

public interface PluginDao {

    public Plugin load (int id);

    public void save(Plugin plg);
}

a implementation of interface is:

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

    }
}

and dulcis in fundo, my PluginTest (for dirty-testing) is:

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

If I now add the method PluginDaoImpl.save (), em.flush line-code I get this error:

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

*the cause is not specificated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-11-26 09:47:07

您缺少
在你的 context.xml 中

(参见 使用@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">

You are missing <tx:annotation-driven transaction-manager="myTransactionManager"/>
in your context.xml

(See Using @Transactional)

Add the tx namespace like this:

<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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文