<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    心的方向

    新的征途......
    posts - 75,comments - 19,trackbacks - 0

    轉自:http://dev.csdn.net/article/68/68297.shtm
    hibernate 三種查詢方式     選擇自 wangyihust 的 Blog 
     

    (一)HQL

    HQLHibernate Qusery Language,如果你已經熟悉它,就會發現它跟SQL非常相像。不過你不要被表面的假象迷惑,HQL是面向對象的(OO,用生命的眼光看待每一個對象,他們是如此鮮活)。如果你對JAVASQL語句有一定了解的話,那么HQL對你簡直易如反掌,你完全可以利用在公車上的時間掌握它。

    以下從幾個方面進行慢慢深入:

    1
    。大小些敏感
    大家知道SQL-92 Query是對大小寫不敏感的,但是在HQL(前面提到它是OO的)中對對象類的名稱和屬性確實大小寫敏感的(符合java編程語法)。

    HQL 子句本身大小寫無關,但是其中出現的類名和屬性名必須注意大小寫區分
    如:sElect cat.name from Cat as catselect cat.name from Cat as cat是一樣的
    但是:
    sElect
    cat.name from CAT as catselect cat.name from Cat as cat確實不一樣的。

    2
    from語句
    最簡單的:
    from eg.Cat
    它只是簡單的返回所有eg.Cat的實例,通常我們此時會為eg.Cat其個別名,因為在query的其余部分可能會用到(參看上邊關于大小寫敏感時的例子情形),如:
    from eg.Cat as cat 這里as可以省略。


    上邊只是單表查詢,多表的情況如下寫法:
    from eg.Cat, eg.Dog
    from eg.Cat as cat, eg.Dog as dog

    3
    join相關
    (inner) join
    left (outer) join
    right (outer) join
    full join
    HQL
    同樣對SQL中的這些特性支持
    下面插播一個小話題,關于上邊的那些特性,我一直都沒怎么用,今天既然說到這里,就想把上邊的幾個特性的用法說一下,也算對自己的一個補充:


    假設有兩個表:部門、員工,下面列舉一些數據:
    員工(Employee)
     ID     Name    DepNo
     001   Jplateau   01
     002    Jony        01
     003   Camel      02

    部門(Department)
     ID  Name
     01  
    研發部
     02   
    營銷部

    Hibernate中我們操縱的都是對象,所以我們操縱的是部門類和員工


    1).(inner) join
    select employee.ID as id1,employee.Name as name1,

    department.ID as id2,department.Name as name2  from Employee as employee

     join  Department as department on employee.DepNo=department.ID (注意到條件語句我用on 沒有用where)
    那么執行結果是什么呢?
    id1 name1 id2 name2
    ++++++++++++++++++++++++++++++++++++++
    001 Jplateau 01
    研發部
    002 Jony 01
    研發部

    2).left (outer) join
    select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
    as name2 from Employee as employee left join Department as department on employee.DepNo=
    department.ID
    那么執行結果又該是什么呢?
    id1 name1 id2 name2
    ++++++++++++++++++++++++++++++++++++++
    001 Jplateau 01
    研發部
    002 Jony 01
    研發部
    003 Camel null null
    {
    就是說此時我要已第一個表的記錄多少為準,第二個表中沒有相應紀錄的時候填充null}
    3). right (outer) join
    select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
    as name2 from Employee as employee right join Department as department on employee.DepNo=
    department.ID
    那么執行結果又該是什么呢?
    id1 name1 id2 name2
    ++++++++++++++++++++++++++++++++++++++
    001 Jplateau 01
    研發部
    002 Jony 01
    研發部
    null null 02
    營銷部
    {
    就是說此時我要已第二個表的記錄多少為準,第一個表中沒有相應紀錄的時候填充null}

    4
    select語句
    就是要確定你要從查詢中返回哪些對象或者哪些對象的屬性。寫幾個例子吧:
    select employee form Employee as employee
    select employee form Employee as employee where employee.Name like 'J%'
    select employee.Name form Employee as employee where employee.Name like 'J%'
    select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
    as name2 from Employee as employee right join Department as department on employee.DepNo=
    department.ID

    select
    elements(employee.Name) from Employee as employee
    (不明白elements到底是做什么用的?望給于說明)
    等等


    5。數學函數
    JDO
    目前好像還不支持此類特性。
    avg(...), sum(...), min(...), max(...)

    count(*)

    count(...), count(distinct ...), count(all...)

    其用法和SQL基本相同

    select distinct employee.name from Employee as employee
    select count(distinct employee.name),count(employee) from Employee as employee

    6
    polymorphism (暫時不知道如何解釋?)
    from com.test.Animal as animal
    不光得到所有Animal得實例,而且可以得到所有Animal的子類(如果我們定義了一個子類Cat
    一個比較極端的例子
    from java.lang.Object as o
    可以得到所有持久類的實例

    7
    where語句
    定義查詢語句的條件,舉幾個例子吧:
    from Employee as employee where employee.Name='Jplateau'
    from Employee as employee where employee.Name like 'J%'
    from Employee as employee where employee.Name like '%u'
    where語句中“=”不光可以比較對象的屬性,也可以比較對象,如:
    select animal from com.test.Animal as animal where animal.name=dog

    8
    。表達式

    SQL語句中大部分的表達式在HQL中都可以使用:
    mathematical operators +, -, *, /

    binary comparison operators =, >=, <=, <>, !=, like

    logical operations and, or, not

    string concatenation ||

    SQL scalar functions like upper() and lower()

    Parentheses ( ) indicate grouping

    in, between, is null

    JDBC IN parameters ?

    named parameters :name, :start_date, :x1
    (這種應該是另一種"?"的變通解決方法)

    SQL literals 'foo', 69, '1970-01-01 10:00:01.0'

    Java public static final constants eg.Color.TABBY

    其他不必解釋了,在這里我只想對查詢中的參數問題說明一下:
    大家知道在SQL中進行傳遞參數進行查詢的時候,我們通常用PreparedStatement,在語句中寫一大堆的“?”,
    hql中也可以用這種方法,如:
    List mates = sess.find(
    "select employee.name from Employee as employee " +
    "where employee.Name=? ",
    name,
    Hibernate.STRING
    );
    (
    說明:上面利用Session里的find方法,在hibernateapi Session中重載了很多find方法,它可以滿足你多種形式的查詢)
    上邊是一個參數的情形,這種情況下緊接著引入參數和定義參數的類型,當為多個參數,調用另一個find方法,它的后兩個
    參數都是數組的形式。

    還有另外一種方法來解決上邊的問題,JDO也有這樣的方法,不過和hibernate的表現形式上有差別,但他們兩個骨子里卻是
    一樣的,如:
    Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
    q.setString("name", "Jplateau");
    //
    當有多個參數的時候在此逐一定義
    Iterator employees = q.iterate();

    9
    order 語句
    sql語句沒什么差別,如:
    select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (
    或者asc)

    10
    group by 語句
    同樣和sql語句沒什么差別,如:

    select employee.name,employee.DepNo from Employee as employee group by employee.DepNo

    select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id
    {Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects.}
    誰幫我解釋一下上邊兩句,謝過!

    11
    。子查詢
    hibernate
    同樣支持子查詢,寫幾個例子:

    from eg.Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.DomesticCat cat )

    (二)條件查詢Criteria  Query

    。數學函數
    JDO
    目前好像還不支持此類特性。
    avg(...), sum(...), min(...), max(...)

    count(*)

    count(...), count(distinct ...), count(all...)

    其用法和SQL基本相同

    select distinct employee.name from Employee as employee
    select count(distinct employee.name),count(employee) from Employee as employee

    6
    polymorphism (暫時不知道如何解釋?)
    from com.test.Animal as animal
    不光得到所有Animal得實例,而且可以得到所有Animal的子類(如果我們定義了一個子類Cat
    一個比較極端的例子
    from java.lang.Object as o
    可以得到所有持久類的實例

    7
    where語句
    定義查詢語句的條件,舉幾個例子吧:
    from Employee as employee where employee.Name='Jplateau'
    from Employee as employee where employee.Name like 'J%'
    from Employee as employee where employee.Name like '%u'
    where語句中“=”不光可以比較對象的屬性,也可以比較對象,如:
    select animal from com.test.Animal as animal where animal.name=dog

    8
    。表達式

    SQL語句中大部分的表達式在HQL中都可以使用:
    mathematical operators +, -, *, /

    binary comparison operators =, >=, <=, <>, !=, like

    logical operations and, or, not

    string concatenation ||

    SQL scalar functions like upper() and lower()

    Parentheses ( ) indicate grouping

    in, between, is null

    JDBC IN parameters ?

    named parameters :name, :start_date, :x1
    (這種應該是另一種"?"的變通解決方法)

    SQL literals 'foo', 69, '1970-01-01 10:00:01.0'

    Java public static final constants eg.Color.TABBY

    其他不必解釋了,在這里我只想對查詢中的參數問題說明一下:
    大家知道在SQL中進行傳遞參數進行查詢的時候,我們通常用PreparedStatement,在語句中寫一大堆的“?”,
    hql中也可以用這種方法,如:
    List mates = sess.find(
    "select employee.name from Employee as employee " +
    "where employee.Name=? ",
    name,
    Hibernate.STRING
    );
    (
    說明:上面利用Session里的find方法,在hibernateapi Session中重載了很多find方法,它可以滿足你多種形式的查詢)
    上邊是一個參數的情形,這種情況下緊接著引入參數和定義參數的類型,當為多個參數,調用另一個find方法,它的后兩個
    參數都是數組的形式。

    還有另外一種方法來解決上邊的問題,JDO也有這樣的方法,不過和hibernate的表現形式上有差別,但他們兩個骨子里卻是
    一樣的,如:
    Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
    q.setString("name", "Jplateau");
    //
    當有多個參數的時候在此逐一定義
    Iterator employees = q.iterate();

    9
    order 語句
    sql語句沒什么差別,如:
    select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (
    或者asc)

    10
    group by 語句
    同樣和sql語句沒什么差別,如:

    select employee.name,employee.DepNo from Employee as employee group by employee.DepNo

    select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id
    {Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects.}
    誰幫我解釋一下上邊兩句,謝過!

    11
    。子查詢
    hibernate
    同樣支持子查詢,寫幾個例子:

    from eg.Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.DomesticCat cat )

    (二)條件查詢Criteria  Query

     Criteria criteria = osession.createCriteria(Owner.class);
       criteria.add(Expression.eq("age", new Integer(100)));
       criteria.setFirstResult(2);                   //從返回結果的第二條記錄開始的5條記錄
       criteria.setMaxResults(5);
       List lc=criteria.list();
       System.out.println("條件查詢");
       System.out.println(lc.size());

    (三)原生SQL語句查詢

    posted on 2008-04-10 17:41 阿偉 閱讀(291) 評論(0)  編輯  收藏 所屬分類: hibernate

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 三年片免费高清版 | 无码日韩精品一区二区免费| 一级一片免费视频播放| 亚洲AV无码一区二区三区在线| 亚洲人成图片小说网站| 国产片免费在线观看| 无码专区永久免费AV网站| 最近2019中文字幕免费大全5| 中文字幕免费在线看电影大全 | 亚洲AV无码成人精品区大在线| 无码国产精品久久一区免费| 亚洲电影免费观看| 免费一级毛片在线播放视频| 最近免费字幕中文大全| 免费无码国产V片在线观看| 相泽南亚洲一区二区在线播放| 亚洲午夜无码久久| 中文字幕无码精品亚洲资源网久久 | 国产亚洲成av片在线观看| 久久亚洲av无码精品浪潮| 亚洲综合区小说区激情区| 亚洲国产婷婷综合在线精品| 又黄又大又爽免费视频| 免费欧洲美女牲交视频| 国产成人免费永久播放视频平台| 在线免费不卡视频| 日本不卡视频免费| 国产免费131美女视频| 国产免费久久精品| 亚洲国产精品丝袜在线观看| 大胆亚洲人体视频| 亚洲精品国产精品乱码不卡| 国产精品亚洲综合专区片高清久久久| 精品亚洲一区二区三区在线播放 | 一级毛片免费播放试看60分钟| 免费毛片毛片网址| 国产精品美女免费视频观看| 久99久无码精品视频免费播放| 十八禁在线观看视频播放免费| 永久免费av无码网站yy| 久久A级毛片免费观看|