//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();
//HQL-Delete
String hql = "delete from Product where name = :name";
Query query = session.createQuery(hql);
query.setString("name","Product 1");
int rowCount = query.executeUpdate();
//HQL-Function
String hql = "select min(product.price), max(product.price) from Product product";
Query query = session.createQuery(hql);
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();
//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();
//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();
//HQL-where
String hql = "from Product where price > 2.0 and name like 'P%'";
Query query = session.createQuery(hql);
List results = query.list();
//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);
【注】
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
查詢語句可以返回值為任何類型的屬性或?qū)ο螅ǚ祷仡愋蜑槟撤N組件(Component)的屬性: 查詢語句可以返回多個(gè)對(duì)象和(或)屬性,存放在 Object[]隊(duì)列中: 或存放在一個(gè)List對(duì)象中: 也可能直接返回一個(gè)實(shí)際的類型安全的Java對(duì)象(假設(shè)類Family有一個(gè)合適的構(gòu)造函數(shù)): 也可以使用關(guān)鍵字as給“被選擇了的表達(dá)式”指派別名:
|