导航

Hibernate 抓取策略

Posted on 2017-10-09 12:12  耍流氓的兔兔  阅读(163)  评论(0编辑  收藏  举报

 

抓取策略:

  为了改变SQL语句执行的方式

  当应用程序需要在Hibernate实体对象的关联关系间进行导航的时候,Hibernate如何获取关联对象的策略

  抓取策略可以在O/R映射的元数据中声明,也可以在特定的HQL或Criteria Query中重载声明

  Hibernate的抓取策略只影响get | load方法,对HQL是不影响的

  参考链接:http://www.cnblogs.com/crazylqy/p/4081459.html

  select fetching:

    默认执行多条sql语句

    可配置在多方

<set name="orders" cascade="save-update, delete" fetch="select">
Hibernate: 
    select
        customer0_.c_id as c_id1_0_0_,
        customer0_.c_name as c_name2_0_0_,
        customer0_.c_gender as c_gender3_0_0_,
        customer0_.c_age as c_age4_0_0_,
        customer0_.c_level as c_level5_0_0_ 
    from
        t_customer customer0_ 
    where
        customer0_.c_id=?
Hibernate: 
    select
        orders0_.customer_id as customer4_1_0_,
        orders0_.id as id1_1_0_,
        orders0_.id as id1_1_1_,
        orders0_.orderno as orderno2_1_1_,
        orders0_.product_name as product_3_1_1_,
        orders0_.customer_id as customer4_1_1_ 
    from
        t_order orders0_ 
    where
        orders0_.customer_id=?
[com.roxy.hibernate.pojo.Order@20765ed5]

    join fetching:

      执行左外连接的SQL语句

      如果在多方查询时 配置,则延迟加载不会生效

 <set name="orders" cascade="save-update, delete" fetch="join">
Hibernate: 
    select
        customer0_.c_id as c_id1_0_0_,
        customer0_.c_name as c_name2_0_0_,
        customer0_.c_gender as c_gender3_0_0_,
        customer0_.c_age as c_age4_0_0_,
        customer0_.c_level as c_level5_0_0_,
        orders1_.customer_id as customer4_1_1_,
        orders1_.id as id1_1_1_,
        orders1_.id as id1_1_2_,
        orders1_.orderno as orderno2_1_2_,
        orders1_.product_name as product_3_1_2_,
        orders1_.customer_id as customer4_1_2_ 
    from
        t_customer customer0_ 
    left outer join
        t_order orders1_ 
            on customer0_.c_id=orders1_.customer_id 
    where
        customer0_.c_id=?
[com.roxy.hibernate.pojo.Order@17f9344b]

    subselect fetching:

      使用子查询查询关联数据

<set name="orders" cascade="save-update, delete" fetch="subselect">
Hibernate: 
    select
        customer0_.c_id as c_id1_0_,
        customer0_.c_name as c_name2_0_,
        customer0_.c_gender as c_gender3_0_,
        customer0_.c_age as c_age4_0_,
        customer0_.c_level as c_level5_0_
    from
        t_customer customer0_
Hibernate:
    select
        orders0_.customer_id as customer4_1_1_,
        orders0_.id as id1_1_1_,
        orders0_.id as id1_1_0_,
        orders0_.orderno as orderno2_1_0_,
        orders0_.product_name as product_3_1_0_,
        orders0_.customer_id as customer4_1_0_
    from
        t_order orders0_
    where
        orders0_.customer_id in (
            select
                customer0_.c_id
            from
                t_customer customer0_
        )