在java中使用JpaTemplate来处理事务?
我已经开始考虑使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了添加
@Transactional
之外,您还需要在applicationContext.xml
中指定
,并且还需要一个名为transactionManager
的 bean,(在您的情况下)类型为org.springframework.orm.jpa.JpaTransactionManager
作为旁注 - 通常不是一个好主意DAO 是交易性的。你最好让你的服务层是事务性的。
Apart from adding
@Transactional
, you need to specify<tx:annotation-driven />
in yourapplicationContext.xml
, and also have a bean calledtransactionManager
, that (in your case) is of typeorg.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.
除了 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.