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