• Hibernate 抓取策略


    抓取策略:

      为了改变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_
            )

      

  • 相关阅读:
    Mybatis入门环境搭建
    sts,eclipse里面配置tomcat
    IDEA-导入本地所需要的jar包
    Java使用poi生成Excel,生成两种表格下拉框
    Java使用poi生成Excel表格
    Java synchronized(this)锁住的是什么
    Java多线程之使用ATM与柜台对同一账户取钱
    Java多线程对同一个对象进行操作
    Java Socket通信实例
    java基本数据类型和引用数据类型的区别
  • 原文地址:https://www.cnblogs.com/roxy/p/7640524.html
Copyright © 2020-2023  润新知