Hibernate HQL查詢必知

Sql代碼
  1. //HQL-Associations   
  2. String hql = "select s.name, p.name, p.price from Product p inner join p.supplier as s";   
  3. Query query = session.createQuery(hql);   
  4. List results = query.list();  
//HQL-Associations
String hql = "select s.name, p.name, p.price from Product p inner join p.supplier as s";
Query query = session.createQuery(hql);
List results = query.list();

 

Sql代碼
  1. //HQL-Delete  
  2. String hql = "delete from Product where name = :name";   
  3. Query query = session.createQuery(hql);   
  4. query.setString("name","Product 1");   
  5. int rowCount = query.executeUpdate();  
//HQL-Delete
String hql = "delete from Product where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Product 1");
int rowCount = query.executeUpdate();

 

Sql代碼
  1. //HQL-Function  
  2. String hql = "select min(product.price), max(product.price) from Product product";   
  3. Query query = session.createQuery(hql);   
  4. List results = query.list();  
//HQL-Function
String hql = "select min(product.price), max(product.price) from Product product";
Query query = session.createQuery(hql);
List results = query.list();

 

Sql代碼
  1. //HQL-Fetch Associations HQL Inner Join  
  2. String hql = "from Supplier s inner join fetch s.products as p";   
  3. Query query = session.createQuery(hql);   
  4. List results = query.list();  
//HQL-Fetch Associations HQL Inner Join
String hql = "from Supplier s inner join fetch s.products as p";
Query query = session.createQuery(hql);
List results = query.list();

 

Sql代碼
  1. //HQL-Named Parameters   
  2. String hql = "from Product where price > :price";   
  3. Query query = session.createQuery(hql);   
  4. query.setDouble("price",2.0);   
  5. List results = query.list();   
  6. String hql = "from Product as product where product.supplier=:supplier";   
  7. Query query = session.createQuery(hql);   
  8. query.setEntity("supplier",supplier);   
  9. List results = query.list();  
//HQL-Named Parameters
String hql = "from Product where price > :price";
Query query = session.createQuery(hql);
query.setDouble("price",2.0);
List results = query.list();
String hql = "from Product as product where product.supplier=:supplier";
Query query = session.createQuery(hql);
query.setEntity("supplier",supplier);
List results = query.list();

 

Sql代碼
  1. //HQL-Update  
  2. String hql = "update Supplier set name = :newName where name = :name";   
  3. Query query = session.createQuery(hql);   
  4. query.setString("name","Supplier Name 1");   
  5. query.setString("newName","s1");   
  6. int rowCount = query.executeUpdate();  
//HQL-Update
String hql = "update Supplier set name = :newName where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Supplier Name 1");
query.setString("newName","s1");
int rowCount = query.executeUpdate();

 

Sql代碼
  1. //HQL-where  
  2. String hql = "from Product where price > 2.0 and name like 'P%'";   
  3. Query query = session.createQuery(hql);   
  4. List results = query.list();  
//HQL-where
String hql = "from Product where price > 2.0 and name like 'P%'";
Query query = session.createQuery(hql);
List results = query.list();

 

Sql代碼
  1. //HQL-Map   
  2. String hql = " select new map(usr.name as userName, usr.password as password) from User usr";   
  3. Query query = session.createQuery(hql);   
  4. List list = query.list();   
  5. Map goods =(Map)list.get(0);  
//HQL-Map
String hql = " select new map(usr.name as userName, usr.password as password) from User usr";
Query query = session.createQuery(hql);
List list = query.list();
Map goods =(Map)list.get(0);


【注】

Sql代碼
  1. String hql = " select new map(usr.name as userName, usr.password as password)   
  2. from com.jason.User usr";   
  3. String hql = " select new map(usr.name as userName, usr.password as password)   
  4. from com.jason.User usr";  
String hql = " select new map(usr.name as userName, usr.password as password) 
 from com.jason.User usr";
String hql = " select new map(usr.name as userName, usr.password as password) 
 from com.jason.User usr";

由於from之前的空格,引起unexpected token: from

查詢語句可以返回值爲任何類型的屬性或對象,包括返回類型爲某種組件(Component)的屬性:

Sql代碼
  1. select cust.name.firstName from Customer as cust   
  2. select cat.mate from Cat cat  
select cust.name.firstName from Customer as cust
select cat.mate from Cat cat

 

查詢語句可以返回多個對象和(或)屬性,存放在 Object[]隊列中:

Sql代碼
  1. select mother, offspr, mate.name  
  2. from DomesticCat as mother   
  3.     inner join mother.mate as mate   
  4.     left join mother.kittens as offspr  
select mother, offspr, mate.name
from DomesticCat as mother
    inner join mother.mate as mate
    left join mother.kittens as offspr


或存放在一個List對象中:

Sql代碼
  1. select new list(mother, offspr, mate.name)   
  2. from DomesticCat as mother   
  3.     inner join mother.mate as mate   
  4.     left outer join mother.kittens as offspr  
select new list(mother, offspr, mate.name)
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr

 

也可能直接返回一個實際的類型安全的Java對象(假設類Family有一個合適的構造函數):

Sql代碼
  1. select new Family(mother, mate, offspr)   
  2. from DomesticCat as mother   
  3.     join mother.mate as mate   
  4.     left join mother.kittens as offspr  
select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr


也可以使用關鍵字as給“被選擇了的表達式”指派別名:

Sql代碼
  1. select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n   
  2. from Cat cat  
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

 

這種做法在與子句select new map一起使用時最有用:

Sql代碼
  1. select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )   
  2. from Cat cat
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章