如何排除@Transactional方法? [英] How to exclude a method to be @Transactional?

查看:519
本文介绍了如何排除@Transactional方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务层,我们得到了

@Service
@Transactional(readOnly = false)
public abstract class GenericService

在存储库层中,我们得到了

In repository layer, we got

public abstract class GenericRepository

在服务中,我们有一个save方法调用存储库的save方法,即

In service we have a save method that calls the save method of repository, which is

@Override
   public void savePersist(T entity) {

      getSession().persist(entity);
   }

我要做的就是将对象实体"保存到休眠会话中以进行进一步处理,而无需在数据库中提交或插入任何语句. 但是实际上发生的是在运行savePersist()之后保存实体".我认为这是因为在服务类上使用了@Transactional批注.但是,我应该怎么做才能在会话中保存实体?

All I want to do is to save the object "entity" into hibernate session for further processing, without commit or any insert statement into database. but what actually happens is saving "entity" after running the savePersist(). I think this because of using @Transactional annotation on service class. But what should I do for save the entity in session?

推荐答案

如果只希望Spring不提交事务,则需要对事务进行角色分配.有几种方法:

When you just want Spring not commit the transaction, then you need to roleback the Transaction. There are several ways:

  • 引发运行时异常(在具有@Transactional批注的Method中)-但这有点滥用角色扮演机制
  • 另一种方法是将交易标记为readOnly:@Transactional(readOnly = true)
  • Throwing a Runtime Exception (within the Method that has the @Transactional annotation) - but this is a bit of abusing the roleback mechanism
  • an other way is to mark the Transaction as readOnly: @Transactional(readOnly = true)

但这两者仅适用于整个会话!

But this both works only for the complete session!

另一种方法是使实体只读:

The other way is to make the entity read only: https://docs.jboss.org/hibernate/orm/3.5/reference/de-DE/html/readonly.html#readonly-api

这篇关于如何排除@Transactional方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆