标签:c style class blog code java
主要参考 :http://4045060.blog.51cto.com/4035060/1088025
<many-to-one name="group" column="group_id" fetch="select" />
另外发送一条select语句抓取当前对象关联实体或集合.
测试代码
Session session = null;
try {
session = HibernateUtils.getSession();
/**
* 只读操作,不使用事务也可进行.Hibernate建议任何操作都使用事务.
*/
User u = (User) session.load(User.class,"402881e6466ae95a01466ae95c4d0004");// 加载User
System.out.println("执行了session.load方法");
System.out.println("[value]" + u.getName());
System.out.println("[value]" + u.getGroup().getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
// 关闭session
session.close();
}
}
执行效果
执行了session.load方法
Hibernate:
select
user0_.id as id0_0_,
user0_.name as name0_0_,
user0_.password as password0_0_,
user0_.createTime as createTime0_0_,
user0_.updateTime as updateTime0_0_,
user0_.group_id as group6_0_0_
from
TEST_User user0_
where
user0_.id=?
[value]james
Hibernate:
select
group0_.id as id1_0_,
group0_.name as name1_0_
from
TEST_Group group0_
where
group0_.id=?
[value]集团2
<many-to-one name="group" column="group_id" fetch="join" />
hibernate会通过select语句使用外连接来加载其关联实体或集合. 此时lazy会失效. 执行结果:一条join语句
测试代码同上
执行效果
执行了session.load方法
Hibernate:
select
user0_.id as id0_1_,
user0_.name as name0_1_,
user0_.password as password0_1_,
user0_.createTime as createTime0_1_,
user0_.updateTime as updateTime0_1_,
user0_.group_id as group6_0_1_,
group1_.id as id1_0_,
group1_.name as name1_0_
from
TEST_User user0_
left outer join
TEST_Group group1_
on user0_.group_id=group1_.id
where
user0_.id=?
[value]james
[value]集团2
Hibernate fetch属性详解,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/redcoatjk/p/3777489.html