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

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

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

    IT人生
    專注于java相關技術.
    posts - 53,comments - 87,trackbacks - 0

     

    HQL查詢語言基礎.....

    1 .from

    1.1單表查詢

    from eg.cat as cat.其中,cat只是一個別名,為了用其他子語句的時候書寫簡單

    1.2多表查詢

    from eg.Cat,eg.Dog
    from eg.Cat as cat,eg.Dog as dog
    2 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}

    3 大小寫敏感

    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方法,在hibernate的api 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 )

     



    減肥瘦身品總匯     值得信賴*脈脈美妝*正品現貨謝絕講價     〓深港商盟〓名品歐衣坊(美國休閑品牌AF系列)     QQ三國游戲幣及道具專賣店     小臉紅紅的瘦身旗艦店
    posted on 2009-04-12 20:15 龍華城 閱讀(310) 評論(0)  編輯  收藏 所屬分類: Hibernate
    主站蜘蛛池模板: 亚洲区日韩精品中文字幕| 亚洲国产成人久久综合一区| 亚洲国产AV一区二区三区四区| 国产精品久久免费| 亚洲性色高清完整版在线观看| 久久午夜羞羞影院免费观看| 久久精品国产亚洲av四虎| 在线成人精品国产区免费| 亚洲AV综合色一区二区三区| 午夜视频免费在线观看| 亚洲精品乱码久久久久久下载| 无码人妻精品中文字幕免费东京热| 亚洲一区二区三区91| 日韩免费在线观看| 视频一区在线免费观看| 中文字幕亚洲图片| 91热久久免费精品99| 亚洲卡一卡二卡乱码新区| 午夜成年女人毛片免费观看| 国产精品亚洲专区无码牛牛| 亚洲中文字幕丝袜制服一区| 成人久久免费网站| 亚洲成aⅴ人片在线观| 免费理论片51人人看电影| 免费国产在线精品一区| 亚洲国产精彩中文乱码AV| 一个人免费高清在线观看| 免费观看四虎精品成人| 亚洲av午夜福利精品一区人妖| 4hu四虎最新免费地址| 美美女高清毛片视频黄的一免费| 精品国产香蕉伊思人在线在线亚洲一区二区| 久久九九免费高清视频| 亚洲成aⅴ人片在线观| 亚洲AV之男人的天堂| 久久久精品免费视频| 亚洲精品9999久久久久无码| 国产AV无码专区亚洲AV手机麻豆 | 永久中文字幕免费视频网站| 美女网站在线观看视频免费的| 亚洲日本在线免费观看|