Hibernate05_延迟加载多对多
注:Hibernate的多对多查询延迟加载默认是开启的
lazy的取值为:true(默认),false, extra
例:查询Course,加载对应的Account。
测试:
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Course course = session.get(Course.class, 1);
System.out.println(course);
结果:
发送了一条SQL
Hibernate:
select
course0_.id as id1_4_0_,
course0_.name as name2_4_0_
from
t_course course0_
where
course0_.id=?
Course [id=1, name=Java]
测试:
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Course course = session.get(Course.class, 1);
System.out.println(course.getAccounts());
结果:
发送了两条SQL
Hibernate:
select
course0_.id as id1_4_0_,
course0_.name as name2_4_0_
from
t_course course0_
where
course0_.id=?
Hibernate:
select
accounts0_.cid as cid2_0_0_,
accounts0_.aid as aid1_0_0_,
account1_.id as id1_3_1_,
account1_.name as name2_3_1_
from
account_course accounts0_
inner join
t_account account1_
on accounts0_.aid=account1_.id
where
accounts0_.cid=?
[Account [id=1, name=张三]]
关闭默认的延迟加载
Course.hbm.xml
<set name="accounts" table="account_course" lazy="false">
<key column="cid"></key>
<many-to-many class="com.blu.entity.Account" column="aid"></many-to-many>
</set>
Account.hbm.xml
<set name="courses" table="account_course" lazy="false">
<key column="aid"></key>
<many-to-many class="com.blu.entity.Course" column="cid"></many-to-many>
</set>
再次测试:
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Course course = session.get(Course.class, 1);
System.out.println(course);
结果:
发送了三条SQL
Hibernate:
select
course0_.id as id1_4_0_,
course0_.name as name2_4_0_
from
t_course course0_
where
course0_.id=?
Hibernate:
select
accounts0_.cid as cid2_0_0_,
accounts0_.aid as aid1_0_0_,
account1_.id as id1_3_1_,
account1_.name as name2_3_1_
from
account_course accounts0_
inner join
t_account account1_
on accounts0_.aid=account1_.id
where
accounts0_.cid=?
Hibernate:
select
courses0_.aid as aid1_0_0_,
courses0_.cid as cid2_0_0_,
course1_.id as id1_4_1_,
course1_.name as name2_4_1_
from
account_course courses0_
inner join
t_course course1_
on courses0_.cid=course1_.id
where
courses0_.aid=?
Course [id=1, name=Java]
测试:
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Course course = session.get(Course.class, 1);
System.out.println(course.getAccounts());
结果:
发送了三条SQL
Hibernate:
select
course0_.id as id1_4_0_,
course0_.name as name2_4_0_
from
t_course course0_
where
course0_.id=?
Hibernate:
select
accounts0_.cid as cid2_0_0_,
accounts0_.aid as aid1_0_0_,
account1_.id as id1_3_1_,
account1_.name as name2_3_1_
from
account_course accounts0_
inner join
t_account account1_
on accounts0_.aid=account1_.id
where
accounts0_.cid=?
Hibernate:
select
courses0_.aid as aid1_0_0_,
courses0_.cid as cid2_0_0_,
course1_.id as id1_4_1_,
course1_.name as name2_4_1_
from
account_course courses0_
inner join
t_course course1_
on courses0_.cid=course1_.id
where
courses0_.aid=?
[Account [id=1, name=张三]]
查询Account同理!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blucoding.blog.csdn.net/article/details/107785201
内容来源于网络,如有侵权,请联系作者删除!