1、檢索單個對象
Query和Criteria都提供了返回單個對象的方法uniqueResult()。先調用setMaxResult(1)方法,把最大檢索數目設爲1,在調用uniqueResult()方法。
Hx hx = (Hx)session.createQuery("from Hx").setMaxResults(1).uniqueResult();
Hx hx = (Hx)session.createCriteria(Hx.class).addOrder(Order.asc("name")).setMaxResults(1).uniqueResult();
與對象屬性綁定
Hx hx = new Hx();
hx.setAge("33");
List list = session.createQuery("from Hx as c where c.age=:age").setProperties(hx).list();
SQL內鏈接
內鏈接就是傳統的鏈接操做,用join鏈接關聯表,on做爲鏈接條件,where指定其餘限定條件的查詢,如:
select hx.name,hx.age,hxhome.home from hx join hxhome on hx.id=hxhome.hxid
SQL左外鏈接
在結果表中包含第一個表中知足的全部紀錄,若是是在鏈接條件上匹配紀錄,則第二個表返回相應的值,不然第二個表返回空值。如:
select hx.name,hx.age,hxhome.home from hx left join hxhome on hx.id=hxhome.hxid
SQL右外鏈接
在結果表中包含第二個表中知足的全部紀錄,若是是在鏈接條件上匹配紀錄,則第一個表返回相應的值,不然第一個表返回空值。如:
select hx.name,hx.age,hxhome.home from hx right outer join hxhome on hx.id=hxhome.hxid 數組
2、Hibernate中各類鏈接
1.迫切左外鏈接
如下兩種檢索方式是等價的,它們都能同時迫切左外鏈接類B和類C:
//HQL迫切左外鏈接檢索方式
from A a left join fetch a.b b left join fetch a.c c where b is not null and c is not null
//QBC迫切左外鏈接檢索方式
List result=session.createCriteria(A.class).setFetchMode("this.b",FetchMode.EAGER).setFetchMode("this.c",FetchMode.EAGER).add(Expression.isNotNull("this.b")).add(Expression.isNotNull("this.c")).list();
迫切左外鏈接HQL:
Session session = HibernateSessionFactory.getSession();
List list = session.createQuery("from Hxhome c left join fetch c.hx h") .list();
for(int i=0;i<list.size();i++)
{
Hxhome hh = (Hxhome)list.get(i);
System.out.println("homeid="+hh.getHxid());
System.out.println("home="+hh.getHome());
System.out.println("hxname="+hh.getHx().getName());
}
迫切左外鏈接QBC:
List list = session.createCriteria(Hxhome.class).setFetchMode("hx", FetchMode.EAGER).add(Expression.like("home","h%")).list();
for(int i=0;i<list.size();i++)
{
Hxhome hh = (Hxhome)list.get(i);
System.out.println("homeid="+hh.getHxid());
System.out.println("home="+hh.getHome());
System.out.println("hxname="+hh.getHx().getName());
}
QBC中的FetchMode
FetchMode.DEFAULT:表示採用映射文件中配置的檢索策略
FetchMode.EAGER:覆蓋映射文件中配置的檢索策略,在程序中顯示指定迫切左外鏈接檢索策略
FetchMode.LAZY:覆蓋映射文件中配置的檢索策略,在程序中顯示指定延遲檢索策略
左外鏈接
HQL:
List list = session.createQuery("from Hxhome c left join c.hx ").list();
for(int i=0;i<list.size();i++)
{
Object[] zmx = (Object[])list.get(i);
Hx hx = (Hx)zmx[1];
Hxhome hh = (Hxhome)zmx[0];
System.out.println("hx="+hx.getName()+"---------hh="+hh.getHome());
}
左外鏈接
QBC不支持左外鏈接
內鏈接
HQL:
List list = session.createQuery("from Hxhome c join c.hx ").list();
for(int i=0;i<list.size();i++)
{
Object[] zmx = (Object[])list.get(i);
Hx hx = (Hx)zmx[1];
Hxhome hh = (Hxhome)zmx[0];
System.out.println("hx="+hx.getName()+"---------hh="+hh.getHome());
}
QBC:
List list =session.createCriteria(Hxhome.class).createAlias("hx", "h").add(Expression.like("home","h%")).list();
for(Iterator it=list.iterator();it.hasNext();)
{
Map map = (Map)it.next();
Hxhome hh = (Hxhome)map.get("this");
Hx hx = (Hx)map.get("h");
------------------------
}
右外鏈接
HQL:
List list = session.createQuery("from Hxhome c right join c.hx h").list();
for(Iterator it=list.iterator();it.hasNext();)
{
Object[] zmx = (Object[])it.next();
Hxhome hh = (Hxhome)zmx[0];
Hx hx = (Hx)zmx[1];
System.out.print(hh.getHome());
System.out.println(hx.getName());
}
右外鏈接
QBC:
不支持右外鏈接
HQL支持各類的鏈接查詢
一、默認狀況
from Hx c where c.name like ‘h%’
二、迫切左外鏈接
from Hx c left join fetch c.hxhome where c.name like ‘h%’
三、左鏈接
from Hx c left join c.hxhome where c.name like ‘h%’
四、迫切內鏈接
from Hx c join fetch c.hxhome where c.name like ‘h%’
五、內鏈接
from Hx c join c.hxhome where c.name like ‘h%’
六、右外鏈接
from Hx c right join c.hxhome where c.name like ‘h%’ session
3、報表查詢
一、投影查詢
二、使用匯集函數
三、分組查詢 函數
投影查詢
投影查詢:指查詢結果僅包含部分實體或實體的部分屬性,經過select關鍵字來實現。select關鍵字用於選擇對象的部分屬性,例如:
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Iterator it = session.createQuery("select c.name,c.age,h.home from Hx c join c.homes h where c.id>0 ").list().iterator();
while(it.hasNext())
{
Object[] row = (Object[])it.next();
String age = (String)row[1];
String home = (String)row[2];
System.out.println("|age="+age+"|home="+home);
}
tx.commit();
session.close();
投影查詢
過濾結果中的重複數據—使用set
Iterator it = session.createQuery("select c.name from Hx c").iterate();
Set set = new HashSet();
while(it.hasNext())
{
String it1 = set.iterator();
while(it1.hasNext())
{
String name1 = (String)it1.next();
System.out.println("name1="+name1);
}
}}} } fetch
投影查詢也可使用distinct關鍵字來過濾重複記錄
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Iterator it = session.createQuery("select distinct c.name from Hx c").iterate();
while(it.hasNext())
{
String c.ID,c.NAME,o.ORDER_NUMBER
from CUSTOMERS c inner join ORDERS o
on c.ID=o.CUSTOMER_ID where o.ORDER_NUMBER like 'T%';
以上查詢語句的查詢結果以下:
+----+------+--------------+
| ID | NAME | ORDER_NUMBER |
+----+------+--------------+
| 1 | Tom | Tom_Order001 |
| 1 | Tom | Tom_Order002 |
| 1 | Tom | Tom_Order003 |
+----+------+--------------+
Query的list()方法返回的集合中包含三個對象數組類型的元素,每一個對象數組表明以上查詢結果的一條記錄。 this
使用匯集函數
在HQL中能夠調用
Count:統計函數
Min:求最小值函數
Max:求最大值函數
Sum:求和函數
Avg:求平均數函數 spa
Count:統計函數
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select count(*) from Hx").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Avg:求平均數函數
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Float count = (Float)session.createQuery("select avg(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Sum:求和函數
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select sum(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Min:求最小值函數 Max:求最大值函數
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Object[] count = (Object[])session.createQuery("select min(c.age),max(c.age) from Hx c").uniqueResult();
String min = (String)count[0];
String max = (String)count[1];
System.out.print("min="+min+"|max="+max);
tx.commit();
session.close(); 對象
分組查詢
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Iterator it = session.createQuery("select c.name,count(c) from Hx c group by c.name").iterate();
while(it.hasNext())
{
Object[] oc = (Object[])it.next();
String count = (Integer)oc[1];
System.out.println(name+":"+count);
}
tx.commit();
session.close(); get