首页 > 系统服务 > 详细

Hibernate fetch属性详解

时间:2014-06-09 16:59:50      阅读:572      评论:0      收藏:0      [点我收藏+]

主要参考 :http://4045060.blog.51cto.com/4035060/1088025

一、hibernate抓取策略单端代理的批量抓取fetch=select(默认)/join)

 

1)保持默认,同fetch="select" 

<many-to-one name="group" column="group_id" fetch="select" />

另外发送一条select语句抓取当前对象关联实体或集合.

测试代码

bubuko.com,布布扣
        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();
            }
        }
bubuko.com,布布扣

 执行效果

bubuko.com,布布扣
执行了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
bubuko.com,布布扣

 

2)设置fetch="join"

 <many-to-one name="group" column="group_id" fetch="join" />

hibernate会通过select语句使用外连接来加载其关联实体或集合. 此时lazy会失效.  执行结果:一条join语句 

测试代码同上

 

执行效果

bubuko.com,布布扣
执行了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
bubuko.com,布布扣

 

 二、hibernate抓取策略(集合代理的批量抓取,fetch=select(默认)/join/subselect)

 

 

Hibernate fetch属性详解,布布扣,bubuko.com

Hibernate fetch属性详解

原文:http://www.cnblogs.com/redcoatjk/p/3777489.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!