Spring Data JPA - 在没有 @Transactional 的情况下获取延迟加载的集合
我的期望是在事务范围内访问集合时应该获取延迟加载的集合.例如,如果我想获取一个集合,我可以调用 foo.getBars.size()
.没有活动的事务应该会导致异常,并带有类似的错误消息
My expectation is that a lazy loaded collection should be fetched when the collection is accessed within a transactional scope. For example, if I want to fetch a collection I can call foo.getBars.size()
. The absence of an active transaction should result in an exception with an error message like
未能延迟初始化条形集合:....无法初始化代理 - 没有会话
failed to lazily initialize a collection of bars: .... could not initialize proxy - no Session
但是,我注意到我的最新应用程序中的行为有所不同.我正在使用带有data-jpa"启动器的 Spring Boot 1.5.1.我过去使用过 Spring Boot,但 data-jpa 启动器对我来说是新的.
However, I noticed that the behavior is different in my latest application. I'm using Spring Boot 1.5.1 with the "data-jpa" starter. I have used Spring Boot in the past, but the data-jpa starter is new for me.
考虑以下情况.我有一个延迟加载的 ManyToMany 集合.
Consider the following case. I have a lazy loaded ManyToMany collection.
@SuppressWarnings("serial")
@Entity
@Table(name = "foo")
public class Foo implements java.io.Serializable {
....
private Set<Bar> bars = new HashSet<Bar>(0);
....
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "foo_bar_map",
joinColumns = {@JoinColumn(name = "foo_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "bar_id", nullable = false, updatable = false)})
public Set<Bar> getBars() {
return this.bars;
}
public void setBar(Set<Bar> bars) {
this.bars = bars;
}
我的服务方法未标记为事务性,但我正在访问延迟加载的集合
My service method is NOT marked as Transactional but I am accessing a lazy loaded collection
@Service
public class FooServiceImpl implements FooService {
@Autowired
private FooRepository fooRepo;
@Override
public FooDTO findById(int fooId) {
Foo foo = fooRepo.findOne(fooId);
// The FooDTO constructor will access foo.getBars()
return new FooDTO(foo);
}
对于 FooDTO 构造函数的上下文
And for context on the FooDTO constructor
public FooDTO(Foo foo) {
...
for (Bar bar : foo.getBars()) {
this.bars.add(bar);
}
}
与我的期望和过去的经验相反,此代码成功执行并获取集合.此外,如果我在我的服务方法中抛出一个断点,我可以单步执行代码并查看我的日志中的 SQL 语句,这些语句在我调用 fooRepo 之后获取条形图.在我调用 fooRepo 之后,我希望交易会被关闭.
Contrary to my expectation and past experience, this code executes successfully and fetches the collection. Further, if I throw a breakpoint in my service method, I can step through the code and see the SQL statements in my logs that fetch the bars after my call to the fooRepo. After my call to fooRepo, I expect the transaction to be closed.
这里发生了什么?
推荐答案
Spring Boot 默认使用 OpenEntityManagerInView 拦截器.您可以通过将属性 spring.jpa.open-in-view
设置为 false 来关闭它.
Spring Boot uses an OpenEntityManagerInView interceptor by default. You can turn it off by setting the property spring.jpa.open-in-view
to false.
参见文档 有关此(和其他)JPA 属性的参考.
See the documentation for the reference about this (and other) JPA properties.
相关文章