20. Hibernate Hibernate Query Language

  • View
    49

  • Download
    3

Embed Size (px)

DESCRIPTION

hibernate tutorial

Text of 20. Hibernate Hibernate Query Language

  • Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    Ni dung trnh by

    Hibernate Query Language (HQL)

    HQL from

    HQL select

    HQL aggregate function

    HQL where

    HQL - Expression

    HQL order by

    HQL group by & having

    HQL sub query 2

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    JDBC - SQL

    JDBC s dng cc cu lnh SQL thc hin truy vn d liu truy vn d liu v cc thao tc cp nht nh thm, xa, sa trn bng d liu.

    thao tc tt cc cu lnh SQL cn quan tm n cc bng, cc dng, cc ct v mi quan h gia cc bng v c bit l h qun tr c s d liu ang lm vic.

    Kt qu tr v ca cu lnh truy vn l danh sch cc dng d liu.

    3

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    Hibernate - HQL

    Hibernate cung cp cc API cho php thc hin tc thao tc cp nht nh thm, xa, sa.

    Hibernate cung cp ngn ng truy vn rt mnh c gi l Hibernate Query Language (HQL).

    HQL c lp h qun tr c s d liu v c Hibernate thng dch sang SQL tng ng trong qu trnh thc thi.

    HQL l ngn ng truy vn theo hng i tng. Kt qu truy vn l i tng

    Hibernate s dng cc lp i tng v cc thuc tnh thay cho cc bng v cc ct.

    4

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL Phn bit hoa thng

    HQL khng phn bit thng hoa ngoi tr

    Tn cc lp i tng

    Cc thuc tnh trong lp i tng

    V d 2 cu truy vn ging nhau

    select s from Sach s SELECT s FROM Sach s

    Select s From Sach s SELECT s FROM Sach s

    V d 2 cu truy vn khc nhau

    select s from sach s SELECT s FROM Sach s

    Select s From SACH s SELECT s FROM Sach s

    5

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL Mnh from

    Ly tt c c i tng danh mc

    from DanhMuc

    select dm from DanhMuc dm

    select dm from DanhMuc as dm

    Ly tt c i tng sch

    from Sach

    select s from Sach s

    select s from Sach as s

    6 Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL Mnh from ly tt c i tng

    7

    1

    2

    3

    String hql = "from Sach"; Query query = session.createQuery(hql); List ds=query.list();

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL Mnh from - phn trang

    8

    1 2 3 4 5

    String hql = "from Sach"; Query query = session.createQuery(hql); query.setFirstResult(3); query.setMaxResults(5); List ds=query.list();

    - Ly t v tr th n (tnh t 0) setFirstResult (int n) - ly ti a m i tng setMaxResults (int m) - Tng t trong MYSQL LIMIT N, M

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - join

    cross join

    inner join hoc join

    left join hoc left outer join

    right join hoc right outer join

    left join fetch hoc left outer join fetch

    right join fetch hoc right outer join fetch

    9

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL cross join

    Thc hin tch cartesian (cn gi l cross join)

    from Sach, DanhMuc

    from Sach s, DanhMuc dm

    select s,dm from Sach s, DanhMuc dm

    Kt qu tr v l mt ArrayList

    S lng phn t = s lng danh mc * s lng sch

    10

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL cross join

    11

    1 2 3 4 5 6 7 8

    String hql = "from Sach s, DanhMuc dm"; Query query = session.createQuery(hql); ArrayList ds = query.list(); for(int i=0; i

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL inner join

    S lng danh mc: 4

    S lng sch: 10 (u thuc danh mc)

    inner join

    from DanhMuc dm

    inner join dm.sachs

    Kt qu

    S lng: 10

    12

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL left join

    S lng danh mc: 4 (2 danh mc khng c sch)

    S lng sch: 10 (u thuc danh mc)

    Left join

    from DanhMuc dm

    left join dm.sachs

    Kt qu

    S lng: 12

    2 danh mc khng c

    sch no thuc v

    13 Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL right join

    S lng danh mc: 4

    S lng sch: 10 (u thuc danh mc)

    Right join

    from DanhMuc dm

    right join dm.sachs

    Kt qu

    S lng: 10

    14

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL left join fetch

    S lng danh mc: 4 (c

    S lng sch: 10 (u thuc danh mc)

    Left join fetch

    from Sach s left join fetch

    s.danhMuc

    Kt qu

    S lng: 10

    15

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL right join fetch

    S lng danh mc: 4 (c

    S lng sch: 10 (u thuc danh mc)

    right join fetch

    from Sach s right join fetch

    s.danhMuc

    Kt qu

    S lng: 10

    16

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    17

    1 2 3

    String hql = "select s.danhMuc from Sach s"; Query query = session.createQuery(hql); List ds=query.list();

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    18

    1

    2

    3

    String hql = "select s.danhMuc.tenDanhMuc from Sach s";

    Query query = session.createQuery(hql);

    List ds=query.list();

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    19

    1 2 3 4 5 6 7 8 9

    String h ="select s.maSach, s.tenSach, s.danhMuc from Sach s"; Query query = session.createQuery(h); List ds=query.list(); for(int i=0; i

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    20

    1 2 3 4

    String hql ="select distinct s.danhMuc.tenDanhMuc from Sach s"; Query query = session.createQuery(hql); List ds=query.list();

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    21

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

    package pojo; public class MyClass { private String maSach; private String tenSach; private DanhMuc danhMuc; public MyClass() { } public MyClass(String maSach, String tenSach, DanhMuc danhMuc) { this.maSach = maSach; this.tenSach = tenSach; this.danhMuc = danhMuc; } //Getters & Setters }

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - select

    22

    1 2 3 4 5 6 7 8 9

    String hql = "select new pojo.MyClass (s.maSach, s.tenSach, s.danhMuc) from Sach s"; Query query = session.createQuery(hql); List ds=query.list(); for(int i=0; i

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - Aggregate functions

    avg

    min

    max

    count

    sum

    23

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - Aggregate functions

    24

    1 2 3 4 5 6 7 8 9 10 11 12 13

    String hql = " select avg(s.giaBan) as GiaTrungBinh," + " count(*) as SoLuongDauSach," + " min(s.giaBan) as GiaBanThapNhat," + " max(s.giaBan) as GiaBanCaoNhat, " + " sum(s.soLuong) as TongSoLuongSach " + " from Sach s"; Query query = session.createQuery(hql); Object[] objs = (Object[]) query.uniqueResult(); double giaTrungBinh=(Double)objs[0]; long soLuongDauSach=(Long)objs[1]; double giaBanThapNhat=(Double)objs[2]; double giaBanCaoNhat=(Double)objs[3]; long tongSoLuongSach=(Long)objs[4];

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - where

    25

    1 2 3 4 5

    String tenSach="Java"; String hql = "from Sach s where s.tenSach like :tenSach"; Query query = session.createQuery(hql); query.setString("tenSach", "%"+tenSach+"%"); ArrayList ds=query.list();

    1 2 3 4 5 6 7 8 9

    String tenDanhMuc="Java"; String giaBan=20000; String hql="from Sach s where s.danhMuc.tenDanhMuc=:tenDanhMuc and s.giaBan>:giaBan" Query query = session.createQuery(hql); query.setString("tenDanhMuc", tenDanhMuc); query.setString("giaBan", giaBan); ArrayList ds=query.list();

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - Expression

    +, -, *, /

    =, >=,

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL - Expression

    concat(...,...)

    current_date(), current_time(), current_timestamp()

    second(...), minute(...), hour(...), day(...), month(...), year(...)

    substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length(), mod()

    coalesce() , nullif()

    str(), ...

    27

    Upload by Cafeitvn.com

  • Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2012

    HQL order by

    28

    1 2 3 4 5 6 7 8 9

    String tenSach="Java"; Str