我使用hibernate spring-data-jpa和querydsl进行分页,并使用@BatchSize(size=10)
只进行一次数据库往返。
@Entity
@Table(name = "appel_offre", catalog = "ao")
public class AppelOffre implements java.io.Serializable {
....
@OneToMany(fetch = FetchType.LAZY, mappedBy = "appelOffre")
@BatchSize(size=10)
public Set<AoActivite> getAoActivites() {
return this.aoActivites;
}
和:
@Entity
@Table(name = "ao_activite", catalog = "ao")
public class AoActivite implements java.io.Serializable {
.....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_ACTIVITE", nullable = false)
@BatchSize(size=10)
public Activite getActivite() {
return this.activite;
}
我的查询
JPAQuery query = new JPAQuery(entityManager).from(ao)
.leftJoin( ao.acheteur, ach ).fetch()
.leftJoin( ao.aoActivites , ao_ac )
.leftJoin( ao_ac.activite , ac )
.offset(...).limit(...).list(..);
但是在日志中有很多往返数据库:
1 - round-trip
.....
Hibernate: select ... from ao.ao_activite aoactivite0_ where aoactivite0_.ID_APPEL_OFFRE in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
2 - round-trip
.....
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
3 - round-trip
.....
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
4 - round-trip
.....
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
5 - round-trip
.....
6 - round-trip
.....
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
7 - round-trip
......
8 - round-trip
.....
Hibernate: select ... from ao.activite activite0_ where activite0_.ID_ACTIVITE=?
9 - round-trip
.....
10 - round-trip
答案 0 :(得分:7)
@BatchSize
对两者都有意义
One-To-Many
和Many-to-One
以及如果是Many-To-One
,我们必须在@Entity
级别上应用它(在我们的情况下是Activite
类的映射)
@Entity
@BatchSize(size=25)
@Table(name = "activite" ...
public class Activite implements java.io.Serializable {
...
请在doc 中查看(下面附有小引号):
...
更容易理解批量提取类/实体。请考虑以下示例:在运行时,您在会话中加载了25个Cat实例,并且每个
的映射中指定批量大小来调整此行为Cat
都有对其所有者的引用,即Person。Person
类使用代理映射,lazy =&#34; true&#34;。如果您现在遍历所有猫并在每个猫上调用getOwner()
,默认情况下,Hibernate将执行25个SELECT语句来检索代理所有者。您可以通过在Person:
<class name="Person" batch-size="10">...</class>
...
答案 1 :(得分:5)
@BatchSize对to-many associations (e.g. @OneToMany)更有意义,而非一对一关系。
使用批量提取,您有(M / N + 1)个数据库往返,其中M是未初始化的多对多关联中的子实体数,N是批量大小。
如果您想要一个且只需要一个辅助数据库往返,则需要使用sub-select fetching。
对于一对一的关联(例如@ManyToOne),请使用JPQL or Criteria JOIN FETCH instead。