加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – @BatchSize但在@ManyToOne案例中有很多往返

发布时间:2020-12-15 01:39:56 所属栏目:大数据 来源:网络整理
导读:我使用hibernate spring-data-jpa和querydsl进行分页,并使用@BatchSize(size = 10)只进行一次数据库往返. @Entity@Table(name = "appel_offre",catalog = "ao")public class AppelOffre implements java.io.Serializable { .... @OneToMany(fetch = FetchTyp

我使用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

并且:

@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对两者都有意义

>一对多和
>多对一

就多对一而言,我们必须在@Entity级别上应用它(在我们的情况下,在Activite类的映射上)

@Entity
@BatchSize(size=25)
@Table(name = "activite" ...
public class Activite implements java.io.Serializable {
...

在doc中查看(下面附带的小引号):

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:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读