简体   繁体   English

Spring Data JPA Lazy加载注释为Eager的集合

[英]Spring Data JPA Lazy loads collections annotated as Eager

I have a project that is using Spring Data repositories to auto generate some of its methods. 我有一个项目正在使用Spring Data存储库自动生成其某些方法。 One such method is one that accepts a couple parameters and returns a list of books. 一种这样的方法是接受几个参数并返回书籍列表的方法。 In the repository, that method looks like this: 在存储库中,该方法如下所示:

List<Book> findBySellerFlagTrueAndPublishedState(PublishedState state);

However, when I use that method, it seems like hibernate is lazy loading properties of the books which are annotated as FetchType.EAGER. 但是,当我使用该方法时,似乎休眠状态是书籍的延迟加载属性,这些属性被标注为FetchType.EAGER。 Also, I have the same issue when I build the query myself using JPA's Criteriabuilder. 另外,当我使用JPA的Criteriabuilder自己构建查询时,也会遇到相同的问题。 When I use the default findOne(Long id) method, those properties are eagerly loaded. 当我使用默认的findOne(Long id)方法时,这些属性会急切地加载。

I'm using JPA 2.0, and am using Hibernate as the implementation. 我正在使用JPA 2.0,并且正在使用Hibernate作为实现。

Here's my annotations on one of the relationships I'm having this issue with: 这是我遇到此问题的一种关系的注释:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="BOOK_THEME", 
joinColumns=@JoinColumn(name="BOOK_ID", referencedColumnName="BOOK_ID"),
inverseJoinColumns=@JoinColumn(name="THEME_ID", referencedColumnName="THEME_ID"))
@OrderColumn(name="THEME_SEQUENCE")
private List<Theme> themes;

The getters and setters are just standard, and it's a unidirectional relationship (there are no books in Theme ). getter和setter只是标准的,并且是单向的关系( Theme中没有书籍)。

I think it must be an issue with how I have my annotations set up. 我认为设置注释的方式一定存在问题。 Does anyone know what I have wrong? 有人知道我错了吗?

I discovered that annotating the themes with @Fetch(FetchMode.SUBSELECT) accomplished my goals, but a better solution was to set hibernate.default_batch_fetch_size in the hibernate properties. 我发现用@Fetch(FetchMode.SUBSELECT)注释主题可以实现我的目标,但是更好的解决方案是在hibernate属性中设置hibernate.default_batch_fetch_size That way I could set it to some number (I chose 100) and it would improve the performance of all lazy and eager fetching for many relationships at once. 这样,我可以将其设置为某个数字(我选择了100),它将提高所有一次懒惰和渴望获取许多关系的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM