在java中使用JpaTemplate来处理事务?

发布于 2024-10-09 12:35:51 字数 1288 浏览 0 评论 0原文

我已经开始考虑使用 JpaTemplate 删除某些应用程序中的一些样板代码的可能性。

我现在有这样的事情:

public class SomeDaoClass {

    public SomeDaoClass( boolean handleTransactionsLocally ) {
            _handleTransactionsLocally = handleTransactionsLocally;
    }

    private void persist( final Object object ) throws MyCustomException {
        try {
            if ( _handleTransactionsLocally ) {
                EntityTransaction transaction = getEntityManager().getTransaction();

                transaction.begin();
                getEntityManager().persist( object );
                transaction.commit();
            }
            else {
                getEntityManager().persist( object );
                getEntityManager().flush();
            }
        }
        catch ( Exception exception ) {
            throw new MyCustomException( exception );
        }
    }
}

当从单元测试运行时,我重写 getEntityManager() 并向构造函数提供 true 。当我将其部署到服务器时,我只需从容器返回注入的实体管理器。

然而,似乎我应该能够做这样的事情:

        _jpaTemplate = new JpaTemplate( getEntityManager() );
        _jpaTemplate.persist( object );

How can I在java代码中指定jpaTemplate应该如何处理事务?我尝试使用 @Transactional 注释该方法,但这似乎不起作用。我需要在 _jpaTemplate 上设置属性吗?这可能吗?

TIA

I have started looking at the possibility of using JpaTemplate to remove some boiler plate code in some of my applications.

I have something like this right now:

public class SomeDaoClass {

    public SomeDaoClass( boolean handleTransactionsLocally ) {
            _handleTransactionsLocally = handleTransactionsLocally;
    }

    private void persist( final Object object ) throws MyCustomException {
        try {
            if ( _handleTransactionsLocally ) {
                EntityTransaction transaction = getEntityManager().getTransaction();

                transaction.begin();
                getEntityManager().persist( object );
                transaction.commit();
            }
            else {
                getEntityManager().persist( object );
                getEntityManager().flush();
            }
        }
        catch ( Exception exception ) {
            throw new MyCustomException( exception );
        }
    }
}

When ran from a unit test, I override the getEntityManager() and provide true to the contructor. When I deploy this to the server, I simply return the injected entity manager from the container.

However, it seems like I should be able to do something like this instead:

        _jpaTemplate = new JpaTemplate( getEntityManager() );
        _jpaTemplate.persist( object );

How can I specify in the java code how the jpaTemplate should handle transactions? I've tried annotating the method with @Transactional but this doesn't seem to work. Do I need to set a property on the _jpaTemplate? Is this even possible?

TIA

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

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

发布评论

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

评论(2

久夏青 2024-10-16 12:35:51

除了添加 @Transactional 之外,您还需要在 applicationContext.xml 中指定 ,并且还需要一个名为 transactionManager 的 bean,(在您的情况下)类型为 org.springframework.orm.jpa.JpaTransactionManager

作为旁注 - 通常不是一个好主意DAO 是交易性的。你最好让你的服务层是事务性的。

Apart from adding @Transactional, you need to specify <tx:annotation-driven /> in your applicationContext.xml, and also have a bean called transactionManager, that (in your case) is of type org.springframework.orm.jpa.JpaTransactionManager

As a sidenote - it is usually not a good idea to make DAOs transactional. You'd better have your service layer transactional instead.

末が日狂欢 2024-10-16 12:35:51

除了 Bozho 的回答之外,您实际上不需要使用 JpaTemplate 来获得声明性事务。

在 Spring 中将 JPA 与声明性事务结合使用的典型方法是使用注入到 @PersistenceContext 带注释的字段中的共享 EntityManager。请参阅13.5。 2 基于普通 JPA 实现 DAO

In addition to Bozho's answer, you actually don't need to use JpaTemplate in order to get declarative transactions.

The typical approach to use JPA with declarative transactions in Spring is to use a shared EntityManager injected into @PersistenceContext-annotated field. See 13.5.2 Implementing DAOs based on plain JPA.

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