Hibernate抓取战略,Hibernate框架基础知识

Hibernate框架学习(十)——查询优化,hibernate框架

HQL语法

1、基本语法

String hql = " from  com.yyb.domain.Customer ";//完整写法
String hql2 = " from  Customer "; //简单写法
String hql3 = " from java.lang.Object ";//查询所有表

2、排序

//排序,和sql一样

  String hql1 = " from  Customer order by cust_id asc ";

3、条件

String hql1 = " from  Customer where cust_id =? ";
String hql2 = " from  Customer where cust_id = :id ";

4、分页

String hql1 = " from  Customer  ";//完整写法
Query query = session.createQuery(hql1);
//limit ?,?
// (当前页数-1)*每页条数
query.setFirstResult(2);
query.setMaxResults(2);

5、聚合查询

String hql1 = " select count(*) from  Customer  ";
String hql2 = " select sum(cust_id) from  Customer  ";
String hql3 = " select avg(cust_id) from  Customer  ";
String hql4 = " select max(cust_id) from  Customer  ";
String hql5 = " select min(cust_id) from  Customer  ";

  Query query = session.createQuery(hql5);
  Number number = (Number) query.uniqueResult();

6、投影查询

    String hql1 = " select cust_name from  Customer  ";
    String hql2 = " select cust_name,cust_id from  Customer  ";
    String hql3 = " select new Customer(cust_id,cust_name) from Customer  ";//查询出来封装到对象中,必须有相应的构造函数和空参构造函数

威尼斯赌场,7、多表查询

//回顾-原生SQL
  // 交叉连接-笛卡尔积(避免)
//        select * from A,B 
    // 内连接
//        |-隐式内连接
//            select * from A,B  where b.aid = a.id
//        |-显式内连接
//            select * from A inner join B on b.aid = a.id
    // 外连接
//        |- 左外
//            select * from A left [outer] join B on b.aid = a.id
//        |- 右外
//            select * from A right [outer] join B on b.aid = a.id
//---------------------------------------------------------------------
//HQL的多表查询
        //内连接(迫切)
        //外连接
//            |-左外(迫切)
//            |-右外(迫切)

7.1内连接

//HQL 内连接 => 将连接的两端对象分别返回.放到数组中.
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c inner join c.linkMens ";
        Query query = session.createQuery(hql);
        List<Object[]> list = query.list();
        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

7.2急于内连续,重返三个指标,另一个指标也被包裹到了这么些目的中。

    @Test
    //HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c inner join fetch c.linkMens ";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

威尼斯赌场 1

7.3 左、右连接

String hql = " from Customer c left join c.linkMens ";

String hql = " from Customer c right join c.linkMens ";

1.  抓取计谋

Hibernate中的多表查询及抓取战术,Hibernate抓取攻略

一、类品级查询

1、get方法:未有别的政策,调用即立即查询数据库加载数据。

威尼斯赌场 2

2、load方法:是在推行时不发送任何SQL语句,重返贰个对象,使用该对象时才试行查询;应用类等级的加载计策。

威尼斯赌场 3

1>延迟加载:仅仅收获,未有应用,不会询问,在应用时才举办查询
2>是或不是对类进行延期加载:能够透过在class成分上计划lazy属性来调控

威尼斯赌场 4

  lazy(暗中同意):true 查询类时会重回代理对象,会在利用性质时依据关系的session查询数据库,加载数据

           加载时不查询,使用时才查询

* *    lazy:false 加载时立刻询问,和get方法未有分别

  结论:为了进步成效,提议使用延缓加载(懒加载)

3>注意:使用懒加载时要保管,调用属性加载数据时,session依然打开的,不然会抛出特别。

威尼斯赌场 5

 Criteria语法

Hibernate抓取战略,Hibernate框架基础知识。 1、基本语法

Criteria c = session.createCriteria(Customer.class);
List<Customer> list = c.list();

2、条件

Criteria c = session.createCriteria(Customer.class);
// c.add(Restrictions.idEq(2l));
c.add(Restrictions.eq("cust_id",2l));
List<Customer> list = c.list();

3、分页

Criteria c = session.createCriteria(Customer.class);
//limit ?,? 
c.setFirstResult(0);
c.setMaxResults(2);

4、排序

Criteria c = session.createCriteria(Customer.class);
c.addOrder(Order.asc("cust_id"));
//c.addOrder(Order.desc("cust_id"));      

5、统计

Criteria c = session.createCriteria(Customer.class);
//设置查询目标
c.setProjection(Projections.rowCount());

1.1 类等级检索

1.Hibernate中的多表查询

二、关联等第查询

1、会集计策

lazy属性:决定是或不是延迟加载
    true(暗中认可):延迟加载,懒加载
    false:立刻加载
    extra:非常懒惰,与懒加载效果基本一致
fetch属性:决定加载战略,使用什么类型的SQL语句加载集结数据
    select(暗中同意):单表查询加载
    join:使用多表查询加载会集
    subselect:使用子查询加载群集

2、关联属性计谋

lazy属性:决定加运载飞机会
  false:马上加载
  proxy:由Customer的类品级加载战术决定

fetch属性:决定加载的SQL语句
  select:使用单表查询
  join:使用多表查询

3、结论

为了进步功用,fetch应挑选select,lazy应选取 true,全体施用暗中同意值。

4、no-session难题消除

推而广之session的作用范围

威尼斯赌场 6

离线查询对象

离线查询即不须求Session对象,能够达成动态拼接查询条件。

     DetachedCriteria dc  = DetachedCriteria.forClass(Customer.class);

        dc.add(Restrictions.idEq(6l));//拼装条件(全部与普通Criteria一致)

        //----------------------------------------------------
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        Criteria c = dc.getExecutableCriteria(session);

        List list = c.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

此时此刻的类的属性获取是不是需求延期。

 1.1SQL中的多表查询

【交叉连接】

select * from A,B;

【内连接】

来得内连接:inner join(inner 能够轻便)

Select * from A inner join B on 条件;

隐式内连接:

Select * from A,B where 条件;

【外连接】

左外连接:left outer join

Select * from A left outer join B on 条件;

右外连接:right outer join

Select * from A right outer join B on 条件;

三、批量抓取

batch-size:抓取群集的数码
       在抓取客商的汇集时,叁回抓取多少个客户的联络员集合

一、类等第查询 1、get方法:未有其它政策,调用即马上查询数据库加载数据。
2、load方法…

抓取战术

抓取战术是当应用程序供给在涉及关系间举行导航的时候,Hibernate怎么着得到涉及对象的攻略。

抓取攻略是Hibernate提高品质的一种花招,能够在取得涉及对象的时候,对出殡和埋葬的言语实行优化,可是反复抓取计策必要和延缓加载一同行使来进步质量。

get:立时搜索。get方法一实行,立时询问全数字段的数据。

1.2Hibernate中的多表连接查询

【交叉连接】

接力连接

【内连接】

来得内三番两次 from Customer c inner join c.linkmans

隐式内接连

迫切内一而再from Customer c inner join fetch c.linkmans

【外连接】

左外连接

右外连接

急迫左外连接

推迟加载

推迟加载是Hibernate关联关系对象私下认可的加载格局,延迟加运载飞机制是为着制止有个别无谓的特性耗费而建议来的,所谓延迟加载便是当在真正要求多少的时候,才真的实行多少加载操作。

延期加载分为两类:类等第延迟 和涉嫌级其余延期。

load:延迟检索。暗许景况,load方法实施后,借使只行使OID的值(查询id)不开展询问,即使要使用其余属性值将查询。(会受到映射文件的布局影响)

2.Hibernate中的抓取战术

类等第延迟

类品级延迟是指查询有些对象的时候,是或不是利用有延期,那么些日常在<class>标签上计划lazy属性。

get方法:未有别的交政战术.调用即马上查询数据库加载数据.

load方法:
应用类品级的加载攻略-懒加载|延迟加载。所以采取load方法去询问的时候,不会登时发送SQL语句,当真正使用该对象的时候,才会发送SQL语句。

  • lazy:true(私下认可值),
    查询类时,会回来代理对象.会在应用质量时,依照关系的session查询数据库.加载数据.
  • lazy:false. load方法会与get方法未有任何差距.调用时即加载数据.

示例:

//懒加载|延迟加载
public class Demo {

    @Test
    // get方法 : 立即加载.执行方法时立即发送sql语句查询结果
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------

        Customer c = session.get(Customer.class, 2l);

        System.out.println(c);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    // load方法(默认):是在执行时,不发送任何sql语句.返回一个对象.使用该对象时,才执行查询.
    // 延迟加载: 仅仅获得没有使用.不会查询.在使用时才进行查询.
    // 是否对类进行延迟加载: 可以通过在class元素上配置lazy属性来控制.
        //lazy:true  加载时,不查询.使用时才查询b
        //lazy:false 加载时立即查询.
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------

        Customer c = session.load(Customer.class, 2l);

        //----------------------------------------------------
        tx.commit();
        session.close();
        System.out.println(c);

    }

<class name="Customer" table="cst_customer" lazy="false" >

类名中凡是有$$的标志符都以代理对象。巩固了Customer具有查询数据库的效果。

威尼斯赌场 7

假诺不想利用延缓加载,能够把这几个类的映射文件上的lazy设置为false。也能够将以此悠久化类改为final修饰。此时不能转移代理类,就能够时延迟加载失效。

敲定:为了提升成效.建议使用延缓加载(懒加载)

只顾:使用懒加载时要力保,调用属性加载数据时,session照旧展开的.不然会抛出极度。

威尼斯赌场 8

get不帮助延迟加载,而load补助。

2.1 延迟加载lazy

lazy延迟加载: 查询的时候不发送sql语句,在应用对象的时候才发送sql语句询问

推迟加载氛围类级别的延期加载论及级其他推迟加载

 关联品级延迟

 关联品级延迟是指查询三个指标的关联对象的时候是还是不是接纳延迟加载。这么些经常在<set>或<many-to-one>上安插lazy属性。

Customer c = session.get(Customer.class, 2l);
Set<LinkMan> linkMens = c.getLinkMens();//关联级别

由此客商询问其涉嫌的联系人对象,在查询联系人的时候是或不是使用延迟加载称为关联级其他延迟。

<set>标签上的lazy日常有八个取值:

  • true:暗许值,选择延迟加载
  • false:检索关联对象的时候,不利用延迟加载
  • extra:及其惰性的

<many-to-one>标签上的lazy常常有多个取值:

  • proxy:暗中同意值,是不是利用延迟加载决议于一的一方上的lazy属性的值。
  • false:检索关联对象的时候,不应用延迟加载
  • no-proxy:不用商量

当查问特定的数据库中荒诞不经的多寡时,get会回到null,而load则抛出极度。

2.1.1类等第的延期加载

运用延缓加载的章程查询有些类的时候是还是不是采取的延期称为是类级其余延期。私下认可值是true。

Customer customer = session.load(Customer.class,1l);//
暗中同意就能够选择延迟加载,这种称为是类等第的推移。

 

类品级延迟加载失效:

* final修饰那么些类,不能够发出代理类,延迟加载就能够失灵。

* 在<class>上配置lazy=”false”

抓取计谋

抓取策略值得是询问某些对象的时候,怎么着抓取其关系对象。这一个也能够因而布署达成。在事关对象的价签上安插fetch属性。关联上就分为是在<set>和<many-to-one>上,也都有不一样的取值。

<set>标签上的fetch日常有四个取值:

  • select:暗中同意值,发送的是日常的select语句询问。
  • join:发送一条急迫左外链接去查询
  • subselect:发送一条子查询语句询问其涉嫌对象。

<many-to-one>标签上的fetch有五个取值:

  • select:暗许值,发送一条普通的select语句询问关联对象。
  • join:发送一条殷切左外链接语句去查询其涉及对象。

fetch和lazy组合就能够生出相当多样的效果与利益。其实不用顾忌,因为fetch借使设置为join,lazy就能够失灵了。

演示:

<!-- 
        lazy属性: 决定是否延迟加载
            true(默认值): 延迟加载,懒加载
            false: 立即加载
            extra: 极其懒惰
        fetch属性: 决定加载策略.使用什么类型的sql语句加载集合数据
            select(默认值): 单表查询加载
            join: 使用多表查询加载集合
            subselect:使用子查询加载集合
     -->
     <!-- batch-size: 抓取集合的数量为3.
             抓取客户的集合时,一次抓取几个客户的联系人集合.
      -->
        <set name="linkMens" fecth="" lazy="'>
            <key column="lkm_cust_id" ></key>
           <one-to-many class="LinkMan" /> 
        </set>        

1、fetch:select,lazy:true的情况

 1 //集合级别的关联
 2     //fetch:select 单表查询
 3     //lazy:true 使用时才加载集合数据.
 4     @Test
 5     public void fun1(){
 6         Session session = HibernateUtils.openSession();
 7         Transaction tx = session.beginTransaction();
 8         //----------------------------------------------------
 9         Customer c = session.get(Customer.class, 2l);
10         Set<LinkMan> linkMens = c.getLinkMens();//关联级别
11         System.out.println(linkMens);
12         //----------------------------------------------------
13         tx.commit();
14         session.close();
15         
16     }

在第9行查询Customer的时候并未立刻查询linkMens,生成sql如下:

select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

在第11行使用linkMens的时候才去数据库查,生成sql如下:

  select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

2、fetch:select,lazy:false的情况

 1 //集合级别的关联
 2         //fetch:select 单表查询
 3         //lazy:false 立即记载集合数据
 4         @Test
 5         public void fun2(){
 6             Session session = HibernateUtils.openSession();
 7             Transaction tx = session.beginTransaction();
 8             //----------------------------------------------------
 9             
10             Customer c = session.get(Customer.class, 2l);
11             
12             Set<LinkMan> linkMens = c.getLinkMens();//关联级别
13             
14             System.out.println(linkMens);
15             
16             //----------------------------------------------------
17             tx.commit();
18             session.close();
19             
20         }

在加载Customer的时候就能够把关系的表的数量查询出来,生成sql如下:

    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

3、fetch:select,lazy:extra的情况

 1         //集合级别的关联
 2         //fetch:select 单表查询
 3         //lazy:extra 极其懒惰.与懒加载效果基本一致. 如果只获得集合的size.
           //只查询集合的size(count语句)
 4         @Test
 5         public void fun3(){
 6             Session session = HibernateUtils.openSession();
 7             Transaction tx = session.beginTransaction();
 8             //----------------------------------------------------
 9             
10             Customer c = session.get(Customer.class, 2l);
11             
12             Set<LinkMan> linkMens = c.getLinkMens();//关联级别
13             
14             System.out.println(linkMens.size());
15             
16             System.out.println(linkMens);
17             
18             //----------------------------------------------------
19             tx.commit();
20             session.close();
21             
22         }

在第10行查询Customer的时候并从未当即查询linkMens,生成sql如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

在第14行要求采取的时候才查询,并且并未把linkMens的数目查询出来。生成sql如下:

Hibernate: 
    select
        count(lkm_id) 
    from
        cst_linkman 
    where
        lkm_cust_id =?

4、fetch:join,lazy:true的情况

 1 //集合级别的关联
 2     //fetch:join    多表查询
 3     //lazy:true|false|extra 失效.立即加载.
 4     @Test
 5     public void fun4(){
 6         Session session = HibernateUtils.openSession();
 7         Transaction tx = session.beginTransaction();
 8         //----------------------------------------------------
 9 
10         Customer c = session.get(Customer.class, 1l);
11 
12         Set<LinkMan> linkMens = c.getLinkMens();//关联级别
13 
14         System.out.println(linkMens.size());
15 
16         System.out.println(linkMens);
17 
18         //----------------------------------------------------
19         tx.commit();
20         session.close();
21 
22     }

在第10行查询Customer的时候linkMens也一并被询问出来,后边的操作不再生成sql了,生成sql如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_,
        linkmens1_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens1_.lkm_id as lkm_id1_1_1_,
        linkmens1_.lkm_id as lkm_id1_1_2_,
        linkmens1_.lkm_gender as lkm_gend2_1_2_,
        linkmens1_.lkm_name as lkm_name3_1_2_,
        linkmens1_.lkm_phone as lkm_phon4_1_2_,
        linkmens1_.lkm_email as lkm_emai5_1_2_,
        linkmens1_.lkm_qq as lkm_qq6_1_2_,
        linkmens1_.lkm_mobile as lkm_mobi7_1_2_,
        linkmens1_.lkm_memo as lkm_memo8_1_2_,
        linkmens1_.lkm_position as lkm_posi9_1_2_,
        linkmens1_.lkm_cust_id as lkm_cus10_1_2_ 
    from
        cst_customer customer0_ 
    left outer join
        cst_linkman linkmens1_ 
            on customer0_.cust_id=linkmens1_.lkm_cust_id 
    where
        customer0_.cust_id=?

5、fetch:subselect,lazy:true的情况

 1 @Test
 2     //fetch: subselect 子查询
 3     //lazy: true 懒加载
 4     public void fun5(){
 5         Session session = HibernateUtils.openSession();
 6         Transaction tx = session.beginTransaction();
 7         //----------------------------------------------------
 8         String  hql = "from Customer";
 9         Query query = session.createQuery(hql);
10         List<Customer> list = query.list();
11         for(Customer c:list){
12             System.out.println(c);
13             System.out.println(c.getLinkMens().size());
14             System.out.println(c.getLinkMens());
15         }
16         //----------------------------------------------------
17         tx.commit();
18         session.close();
19     }

在第10行查询Customer的时候并未马上查询linkMens,生成sql如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_,
        customer0_.cust_name as cust_nam2_0_,
        customer0_.cust_source as cust_sou3_0_,
        customer0_.cust_industry as cust_ind4_0_,
        customer0_.cust_level as cust_lev5_0_,
        customer0_.cust_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_

当实行到第13行时,使用子查询获得linkMens的数量,生成sql如下:

Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

6、fetch:subselect,lazy:false的情况

 1  @Test
 2     //fetch: subselect 子查询
 3     //lazy: false 立即加载
 4     public void fun6(){
 5         Session session = HibernateUtils.openSession();
 6         Transaction tx = session.beginTransaction();
 7         //----------------------------------------------------
 8         String  hql = "from Customer";
 9         Query query = session.createQuery(hql);
10         List<Customer> list = query.list();
11         for(Customer c:list){
12             System.out.println(c);
13             System.out.println(c.getLinkMens().size());
14             System.out.println(c.getLinkMens());
15         }
16         //----------------------------------------------------
17         tx.commit();
18         session.close();
19     }

实施第10行时,会询问出装有数据,生成sql如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_,
        customer0_.cust_name as cust_nam2_0_,
        customer0_.cust_source as cust_sou3_0_,
        customer0_.cust_industry as cust_ind4_0_,
        customer0_.cust_level as cust_lev5_0_,
        customer0_.cust_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_
Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

7、fetch:subselect,lazy:extra的情况

 1 @Test
 2     //fetch: subselect 子查询
 3     //lazy: extra 极其懒惰
 4     public void fun7(){
 5         Session session = HibernateUtils.openSession();
 6         Transaction tx = session.beginTransaction();
 7         //----------------------------------------------------
 8         String  hql = "from Customer";
 9         Query query = session.createQuery(hql);
10         List<Customer> list = query.list();
11         for(Customer c:list){
12             System.out.println(c);
13             System.out.println(c.getLinkMens().size());
14             System.out.println(c.getLinkMens());
15         }
16         //----------------------------------------------------
17         tx.commit();
18         session.close();
19 
20     }

施行第10行时,查询Customer对象,linkMens未查询,生成SQL语句如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_,
        customer0_.cust_name as cust_nam2_0_,
        customer0_.cust_source as cust_sou3_0_,
        customer0_.cust_industry as cust_ind4_0_,
        customer0_.cust_level as cust_lev5_0_,
        customer0_.cust_linkman as cust_lin6_0_,
        customer0_.cust_phone as cust_pho7_0_,
        customer0_.cust_mobile as cust_mob8_0_ 
    from
        cst_customer customer0_

执行到13行时,生成sql语句如下:

Hibernate: 
    select
        count(lkm_id) 
    from
        cst_linkman 
    where
        lkm_cust_id =?

施行到14行时,生成sql语句如下:

Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_gender as lkm_gend2_1_1_,
        linkmens0_.lkm_name as lkm_name3_1_1_,
        linkmens0_.lkm_phone as lkm_phon4_1_1_,
        linkmens0_.lkm_email as lkm_emai5_1_1_,
        linkmens0_.lkm_qq as lkm_qq6_1_1_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_1_,
        linkmens0_.lkm_memo as lkm_memo8_1_1_,
        linkmens0_.lkm_position as lkm_posi9_1_1_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id=?

进行第二回的时候,全部数据都加载好了,就不会再去数据库中查了。

1.2  关联等级检索

2.1.2提到级其余推迟加载

查询到有个别对象现在,得到其涉嫌的对象。查询其涉及对象的时候是还是不是使用的推迟。称为是事关级其他延迟。

Customer c = session.get(Customer.class,1l);

c.getLinkMans(); // 查询关联对象的时候,是否利用延迟加载。

涉嫌级其他推迟往往会与抓取战略一齐利用,优化程序。(关联级其他延期在<set>可能是<many-to-one>标签上的推移加载)

关联属性攻略

地点的代码发转过来,获取linkMan,然后加载Customer。

示例:

<!-- 
        fetch 决定加载的sql语句
            select: 使用单表查询(默认值)
            join : 多表查询
        lazy  决定加载时机
            false: 立即加载
            proxy: 由customer的类级别加载策略决定.(默认值)
         -->
        <many-to-one name="customer" column="lkm_cust_id" class="Customer" fetch="" lazy=""  >
        </many-to-one>

1、fetch:select,lazy:proxy的情况

 1     @Test
 2     //fetch:select    单表查询
 3     //lazy:proxy  
 4         //customer-true 懒加载
 5     public void fun1(){
 6         Session session = HibernateUtils.openSession();
 7         Transaction tx = session.beginTransaction();
 8         //----------------------------------------------------
 9         
10         LinkMan lm = session.get(LinkMan.class, 1l);
11         
12         Customer customer = lm.getCustomer();
13         
14         System.out.println(customer);
15         
16         //----------------------------------------------------
17         tx.commit();
18         session.close();
19         
20     }

进行到第10行时,获取linkman,生成SQL如下:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?

鉴于Customer类上未做布署,全体暗中认可使用延缓加载。当实行到12行时,生成SQL如下:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

2、fetch:select,lazy:proxy,Customer上配置lazy=”false”的情况

依然以上代码,不过在Customer的Class配置上投入lazy=”false”,再一次施行。当实践到10行时,生成SQL如下:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkman0_ 
    where
        linkman0_.lkm_id=?
Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

3、fetch:select,lazy:false的图景,同地点2的境况同样。

4、fetch:join,lazy:proxy的情况

 1  @Test
 2     //fetch:join    多表
 3     //lazy: 失效
 4     public void fun3(){
 5         Session session = HibernateUtils.openSession();
 6         Transaction tx = session.beginTransaction();
 7         //----------------------------------------------------
 8         LinkMan lm = session.get(LinkMan.class, 3l);
 9         Customer customer = lm.getCustomer();
10         System.out.println(customer);
11         //----------------------------------------------------
12         tx.commit();
13         session.close();
14 
15     }

当实行到第8行时,生成SQL语句如下:

Hibernate: 
    select
        linkman0_.lkm_id as lkm_id1_1_0_,
        linkman0_.lkm_gender as lkm_gend2_1_0_,
        linkman0_.lkm_name as lkm_name3_1_0_,
        linkman0_.lkm_phone as lkm_phon4_1_0_,
        linkman0_.lkm_email as lkm_emai5_1_0_,
        linkman0_.lkm_qq as lkm_qq6_1_0_,
        linkman0_.lkm_mobile as lkm_mobi7_1_0_,
        linkman0_.lkm_memo as lkm_memo8_1_0_,
        linkman0_.lkm_position as lkm_posi9_1_0_,
        linkman0_.lkm_cust_id as lkm_cus10_1_0_,
        customer1_.cust_id as cust_id1_0_1_,
        customer1_.cust_name as cust_nam2_0_1_,
        customer1_.cust_source as cust_sou3_0_1_,
        customer1_.cust_industry as cust_ind4_0_1_,
        customer1_.cust_level as cust_lev5_0_1_,
        customer1_.cust_linkman as cust_lin6_0_1_,
        customer1_.cust_phone as cust_pho7_0_1_,
        customer1_.cust_mobile as cust_mob8_0_1_ 
    from
        cst_linkman linkman0_ 
    left outer join
        cst_customer customer1_ 
            on linkman0_.lkm_cust_id=customer1_.cust_id 
    where
        linkman0_.lkm_id=?

(1)fetch=”select”

2.2抓取计谋

  抓取计策指的是查找到有个别对象后,抓取其关联的对象的时候利用的计谋。抓取攻略就是在事关对象的配备上(<set>和<many-to-one>)配置fetch属性。

结论:为了提升功效,fetch的挑选上应选select,lazy采用true。所以任何应用私下认可值。

lazy=”true”:默许是懒 加载

2.2.1 在set上配置的lazy和fetch

fetch:抓取计谋,调节SQL语句的发送的格式。

    * select    :暗许值。发送一条select语句询问关联对象。

    * join  :发送一条热切左外连接查询关联对象。

    * subselect :发送一条子查询查询关联对象。

 

lazy:延迟加载,调节SQL语句的发送的时候。

    * true  :默许值。采纳延迟加载。

    * false :不采纳延迟加载。

    * extra :及其懒惰。

no-session问题

推迟加载的主题素材是指当大家调用完action中的某些方法,在jsp页面要突显大家想要的音讯的时候,发掘在dao中打开的session已经倒闭了。

减轻格局:扩展session的效劳范围。能够在过滤器中放行以前张开session,放行之后关闭session。

威尼斯赌场 9

lazy=”false”:会登时发送sql名句去询问关联的数目

2.2.2 在many-to-one上安排的lazy和fetch

fetch:抓取计谋,调控SQL语句的发送的格式。

    * select    :暗中同意值.发送一条select语句询问关联对象。

    * join  :发送一条急切左外连接查询关联对象。

lazy:延迟加载,调控SQL的发送的空子。

    * proxy :暗许值。是不是选用延迟,须求由另一方类上的延期加载来支配。

    * false :不接纳延迟加载。

    * no-proxy:

批量抓取

 在抓取的国策中有一种名字为批量抓取,正是相同的时间询问五个对象的涉及对象时,能够使用批量抓取进行优化。当然这些就不是特别重大了。通过布置batch-size来产生。

示例:

 1  @Test
 2         public void fun10(){
 3             Session session = HibernateUtils.openSession();
 4             Transaction tx = session.beginTransaction();
 5             //----------------------------------------------------
 6 
 7             String hql = "from Customer ";
 8             Query query = session.createQuery(hql);
 9             List<Customer> list = query.list();
10 
11             for(Customer c:list){
12                 System.out.println(c.getLinkMens());
13             }
14 
15             //----------------------------------------------------
16             tx.commit();
17             session.close();
18         }

在尚未计划批量抓取以前,每回试行到第12行时,就能够生成SQL语句去数据库查,循环几回,将在查询几遍。

开展批量抓取配置之后,重新施行上边代码:

 <!-- batch-size: 抓取集合的数量为3.
             抓取客户的集合时,一次抓取几个客户的联系人集合.
      -->
        <set name="linkMens" batch-size="3"  >
            <key column="lkm_cust_id" ></key>
            <one-to-many class="LinkMan" />
        </set>

生成SQL语句如下:

Hibernate: 
    select
        linkmens0_.lkm_cust_id as lkm_cus10_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_1_,
        linkmens0_.lkm_id as lkm_id1_1_0_,
        linkmens0_.lkm_gender as lkm_gend2_1_0_,
        linkmens0_.lkm_name as lkm_name3_1_0_,
        linkmens0_.lkm_phone as lkm_phon4_1_0_,
        linkmens0_.lkm_email as lkm_emai5_1_0_,
        linkmens0_.lkm_qq as lkm_qq6_1_0_,
        linkmens0_.lkm_mobile as lkm_mobi7_1_0_,
        linkmens0_.lkm_memo as lkm_memo8_1_0_,
        linkmens0_.lkm_position as lkm_posi9_1_0_,
        linkmens0_.lkm_cust_id as lkm_cus10_1_0_ 
    from
        cst_linkman linkmens0_ 
    where
        linkmens0_.lkm_cust_id in (
            ?, ?, ?
        )

那会儿壹回会拿3条数据,而不会每趟都到数据库中查了。

 

共同点:

2.2.3 批量抓取

  批量抓取:查询了四个客商的时候,查询五个顾客下的兼具联系人。

  在Customer.hbm.xml中<set>上配置batch-size=”n”

 

1.Hibernate中的多表查询 1.1SQL中的多表查询 【交叉连接】 select * from
A,B; 【内连接】 显示…

lazy=”true” VS lazy
=”extra”:都以懒加载,唯有利用的时候才会去发送sql语句去询问

差别:查询订单数的例子

lazy=”true”:会发送查询全数数据的sql语句

lazy =”extra”:只会发送查询条数的sql语句

(2)fetch=”join”

无论lazy属性设置任何值,都不会有机能,它都会用殷切左外连接去查询关联的数码。

fetch=”subselect”

lazy=”true”:暗中认可是懒 加载

lazy=”false”:会即时发送sql名句去查询关联的数码

共同点:

lazy=”true” VS lazy
=”extra”:都是懒加载,独有应用的时候才会去发送sql语句去询问

差异:查询订单数的事例

lazy=”true”:会发送查询全体数据的sql语句

lazy =”extra”:只会发送查询条数的sql语句

(3)fetch=”subselect” VS fetch=”select”

事例:查询顾客的订单消息

fetch=”select” :发送多条select语句去询问

fetch=”subselect”:只发送一条select语句,子查询的艺术。

2.  HQL

HQL(Hibernate Query Language)描述对象操作的一种查询语言。

与SQL不相同的是,HQL是面向对象的查询,查询的是目的和指标中的属性

留意:HQL中的关键字不区分轻重缓急写,不过类名和总体性名分别轻重缓急写

问答函数:

//获取数据

//获取唯一的值

(1)查询全部:from Teacher

(2)查询单个:select t from Teacher t where t.id = 1

(3)投影:查询部分字段:select t.id,t.name from Teacher t

(4)排序:from Teacher t order by t.id desc/asc(降序/升序)

(5)分页:

//初始地方

query.setFirstResult(0);

//设置一页个数

query.setMaxResults(4);

(6)绑定参数:

方式一:from Teacher t where t.id=?

//与SQL不相同,这里是从0开首的

query.setInteger(0,6);

方式二:from Teacher t where t.id=:id

query.setInteger(“id”,14);

(7)聚合函数:

select count(*) from Teacher

select sum(t.id) from Teacher t

(8)连接查询:

1.陆陆续续查询 (笛Carl积:就是把A B两张表的具有组成打字与印刷出来)

from Teacher t,Classes c

2.隐式内查询

from Father f,Son s where s.father=f

3.显式内查询

from Father f inner join f.sonSet

4.归心似箭内查询

from Father f inner join fetch f.sonSet

(9)命名查询

1.配置

a)全局:*.hbm.xml   HQL语句

b)局部: HQL

2.获得

a)全局:

session.getNamedQuery(“queryName”)

b)局部:

session.getNamedQuery(“className.queryName”)供给利用类的全限定称号

admin

网站地图xml地图