Spring Data JPA-无需@Transactional即可获取延迟加载的集合 [英] Spring Data JPA - Lazy Loaded collection fetched without @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.
请考虑以下情况.我有一个懒惰的ManyManMany集合.
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.
这篇关于Spring Data JPA-无需@Transactional即可获取延迟加载的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!