@BatchSize,但在获取@ManyToOne关联时有许多往返 [英] @BatchSize but many round trips when fetching a @ManyToOne association

查看:88
本文介绍了@BatchSize,但在获取@ManyToOne关联时有许多往返的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将分页与休眠的spring-data-jpa和querydsl一起使用,并且我使用@BatchSize(size=10)仅进行一次数据库往返.

I use pagination with hibernate spring-data-jpa and querydsl and i use @BatchSize(size=10) to make just one round-trip to the database.

@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(..);

但是在日志中有很多往返数据库的行程:

but in the log got many round trip to the database:

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也是
  • One-To-Many and
  • Many-to-One as well

Many-To-One而言,我们必须将其应用于@Entity级别(在我们的情况下,是针对Activite类的映射)

Just in case of Many-To-One, we have to apply it on @Entity level (in our case on mapping of the Activite class)

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

在doc (在下面附加小引用)中检查它:

...

批量提取类/实体更容易理解.考虑以下示例:在运行时,您在一个Session中加载了25个Cat实例,并且每个Cat都有一个对其所有者Person的引用. Person类与代理lazy ="true"映射.如果现在遍历所有猫并在每只猫上调用getOwner(),则默认情况下,Hibernate将执行25条SELECT语句以检索代理所有者.您可以通过在Person映射中指定一个批处理大小来调整此行为:

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>

...

这篇关于@BatchSize,但在获取@ManyToOne关联时有许多往返的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆