16. J2EE Hibernate 4 Hibernate Query Language

  • View
    170

  • Download
    8

Embed Size (px)

Text of 16. J2EE Hibernate 4 Hibernate Query Language

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 query2

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

JDBC - SQL JDBC truy thao bng s dng cc cu lnh SQL thc hin vn d liu truy vn d liu v cc tc cp nht nh thm, xa, sa trn 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

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

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

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

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

HQL Mnh from ly tt c i tng1 2 3 String hql = "from Sach"; Query query = session.createQuery(hql); List ds=query.list();

7

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

HQL Mnh from1 2 3 4 5

- phn trang

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

8

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

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

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

HQL cross join1 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