gpt4 book ai didi

java - @BatchSize 但在获取 @ManyToOne 关联时有很多往返

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:40 24 4
gpt4 key购买 nike

我将分页与 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

最佳答案

@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 {
...

在文档中查看 (下面附有小引用):

20.1.5. Using batch fetching

...

Batch fetching for classes/entities is easier to understand. Consider the following example: at runtime you have 25 Cat instances loaded in a Session, and each Cat has a reference to its owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and call getOwner() on each, Hibernate will, by default, execute 25 SELECT statements to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:

<class name="Person" batch-size="10">...</class>

...

关于java - @BatchSize 但在获取 @ManyToOne 关联时有很多往返,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com