关于hiberante FetchMode

以Person, Address 为例, 其关系为一对多,数据库中有3条person, 每个分别对应2条address。查询语句如下:

Criteria c = session.createCriteria(Person.class);
c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Person> list = (List<Person>)c.list();
for (Person p : list) {
        System.out.println(p.getName());
			
	Set<Address> addressSet = p.getAddressSet();
	for (Address a : addressSet) {
		System.out.println(a);
	}			
}


1 默认不设置FetchMode
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "P_ID")
public Set<Address> getAddressSet() {
	return addressSet;
}


结果生成如下
Hibernate: select this_.PERSON_ID as PERSON1_0_0_, this_.PERSON_NAME as PERSON2_0_0_ 
from PERSON this_
P1
Hibernate: select addressset0_.P_ID as P3_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_0_, addressset0_.ADDRESS_NAME as ADDRESS2_1_0_ 
from ADDRESS addressset0_ 
where addressset0_.P_ID=?
2	P1 A2
1	P1 A1
P2
Hibernate: select addressset0_.P_ID as P3_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_0_, addressset0_.ADDRESS_NAME as ADDRESS2_1_0_ 
from ADDRESS addressset0_ 
where addressset0_.P_ID=?
4	P2 A2
3	P2 A1
P3
Hibernate: select addressset0_.P_ID as P3_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_0_, addressset0_.ADDRESS_NAME as ADDRESS2_1_0_ 
from ADDRESS addressset0_ 
where addressset0_.P_ID=?
5	P3 A1
6	P3 A2


即:先查所有Person的id, 然后根据id查对应的Address。产生N+1问题

2. FetchMode.SELECT

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "P_ID")
public Set<Address> getAddressSet() {
	return addressSet;
}

效果同默认,即hibernate默认的FetchMode是SELECT

3. FetchMode.JOIN
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "P_ID")
public Set<Address> getAddressSet() {
	return addressSet;
}

效果如下
Hibernate: select this_.PERSON_ID as PERSON1_0_1_, this_.PERSON_NAME as PERSON2_0_1_, addressset2_.P_ID as P3_3_, addressset2_.ADDRESS_ID as ADDRESS1_3_, addressset2_.ADDRESS_ID as ADDRESS1_1_0_, addressset2_.ADDRESS_NAME as ADDRESS2_1_0_ 
from PERSON this_ 
   left outer join ADDRESS addressset2_ 
        on this_.PERSON_ID=addressset2_.P_ID

P1
2	P1 A2
1	P1 A1
P2
4	P2 A2
3	P2 A1
P3
5	P3 A1
6	P3 A2


采用外联,用一条sql取出person及其address

4. FetchMode.SUBSELECT
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
@JoinColumn(name = "P_ID")
public Set<Address> getAddressSet() {
	return addressSet;
}


效果
Hibernate: select this_.PERSON_ID as PERSON1_0_0_, this_.PERSON_NAME as PERSON2_0_0_ 
from PERSON this_
P1
Hibernate: select addressset0_.P_ID as P3_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_0_, addressset0_.ADDRESS_NAME as ADDRESS2_1_0_ 
from ADDRESS addressset0_ 
where addressset0_.P_ID in (select this_.PERSON_ID from PERSON this_)
2	P1 A2
1	P1 A1
P2
4	P2 A2
3	P2 A1
P3
6	P3 A2
5	P3 A1

生成2条sql, 第二句用in 查关联Address数据


5. batchSize
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@BatchSize(size=4)
@JoinColumn(name = "P_ID")
public Set<Address> getAddressSet() {
	return addressSet;
}

效果
Hibernate: select this_.PERSON_ID as PERSON1_0_0_, this_.PERSON_NAME as PERSON2_0_0_ 
from PERSON this_
P1
Hibernate: select addressset0_.P_ID as P3_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_, addressset0_.ADDRESS_ID as ADDRESS1_1_0_, addressset0_.ADDRESS_NAME as ADDRESS2_1_0_ 
from ADDRESS addressset0_ 
where addressset0_.P_ID in (?, ?, ?)
1	P1 A1
2	P1 A2
P2
4	P2 A2
3	P2 A1
P3
5	P3 A1
6	P3 A2


生成2条sql,第二条用in。 这里数据库中共有3条person。
如果设size=0或1, 则效果同select一样,产生N+1问题。
如size=2, 生成3条sql,第一条相同,第二条为
select 。。。。
from ADDRESS addressset0_ 
where addressset0_.P_ID in (?, ?)

第三条为
select。。
from ADDRESS addressset0_ 
where addressset0_.P_ID=?


如果设size=3则生成2条sql,第一条相同,第一条为
select 。。。。
from ADDRESS addressset0_ 
where addressset0_.P_ID in (?, ?, ?)

由上,size即为in中数据个数。

你可能感兴趣的:(Hibernate)