HQL的使用详解
package com.inspur.test;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import com.inspur.po.TAddress;
import com.inspur.po.TUser;
import junit.framework.TestCase;
public class HqlTest extends TestCase {
?Session session=null;
?@Override
?protected void setUp() throws Exception {
??Configuration config=new Configuration().configure();
??SessionFactory sessionFactory=config.buildSessionFactory();
??session=sessionFactory.openSession();
?}
?@Override
?protected void tearDown() throws Exception {
??session.close();
?}
?//目的是獲得對(duì)象的部分屬性,不用全部加載獲取對(duì)象,可以只捕獲其屬性即可,
?//如果使用criteria則load上來(lái)的是全部的對(duì)象,從對(duì)象中在獲得屬性,增加了系統(tǒng)負(fù)擔(dān)。
?public void testProperty(){
??String hql="select user.name from TUser user";
??List list=session.createQuery(hql).list();//返回的list中的每一個(gè)條目保存的是一個(gè)string類(lèi)型的字符串而不是TUser類(lèi)對(duì)象。
??Iterator it=list.iterator();
??while(it.hasNext()){
???System.out.println(it.next());
??}
??
?}
?//多屬性的獲取后返回的list中每一個(gè)條目保存是一個(gè)object[]類(lèi)型的對(duì)象數(shù)組,
?//數(shù)組中的元素是對(duì)應(yīng)hql中的屬性的順序
?public void testMulProperties(){
??String hql="select user.name,user.age from TUser user";
??List list=session.createQuery(hql).list();
??Iterator it=list.iterator();
??while(it.hasNext()){
???Object[]result=(Object[])it.next();
???System.out.println(result[0]+" "+result[1]);
??}
?}
?public void testCompute(){
??String hql="select count(*),min(user.age) from TUser user";
??List list=session.createQuery(hql).list();
??Iterator it=list.iterator();
??System.out.println("====test compute function====");
??while(it.hasNext()){
???Object[]result=(Object[])it.next();
???System.out.println(result[0]+" "+result[1] );
??}
?}
?public void testSave(){
??Transaction tran=null;
??TUser user=null;
??String hql="from TUser user where user.name='liuzhq'";
??Query query=session.createQuery(hql);
??List list=query.list();
??Iterator it=list.iterator();
??tran=session.beginTransaction();
??while(it.hasNext()){
???
???user=(TUser)it.next();
???user.setAge(24);
???session.save(user);
???tran.commit();
???System.out.println("save successfully");
???
??}
??
?}
//?public void testSaveCriteria(){
//??Transaction tran=null;
//??tran=session.beginTransaction();
//??TUser user=null;
//??Criteria criteria=session.createCriteria(TUser.class);
//??criteria.add(Expression.eq("name", "liuzhq"));
//??List list=criteria.list();
//??Iterator it=list.iterator();
//??while(it.hasNext()){
//???user=(TUser)it.next();
//???user.setName("liuzhiqiang");
//???session.save(user);
//???tran.commit();
//???
//??}
//??
//??
//?}
?public void testUpdate(){
??Transaction tran=null;
??tran=session.beginTransaction();
??String hql="update TUser user set user.age=18 where user.name='liuzhiqiang'";
??Query query=session.createQuery(hql);
??query.executeUpdate();
??tran.commit();
??
??
?}
//?public void testDelete(){
//??Transaction tran=null;
//??tran=session.beginTransaction();
//??String hql="delete TUser user where user.age=18";
//??Query query=session.createQuery(hql);
//??query.executeUpdate();
//??tran.commit();
//??
//?}
?//group by 和order by字句的使用,order by字句必須在整個(gè)hql語(yǔ)句的末尾,否則產(chǎn)出錯(cuò)誤輸出
?public void testGroup(){
??String hql="select user.age,count(*)from TUser user? group by user.age having count(*)>? order by user.age desc";
??Query query=session.createQuery(hql);
??query.setInteger(0, 1);
??List list=query.list();
??Iterator it=list.iterator();
??System.out.println("group by");
??while(it.hasNext()){
???Object[]result=(Object[])it.next();
???System.out.println(result[0]+" "+result[1]);
??}
?}
?//通過(guò)query接口進(jìn)行參數(shù)填充,使用占位符相對(duì)于引用參數(shù)綁定和javabean參數(shù)綁定而言是最佳的參數(shù)綁定方式。
?public void testzhan(){
??String hql="from TUser user where user.name=? and user.age=?";
??Query query=session.createQuery(hql);
??query.setString(0, "liuzhiqiang");
??query.setInteger(1, 18);
??List list=query.list();
??Iterator it=list.iterator();
??while(it.hasNext()){
???TUser user=(TUser)it.next();
???System.out.println(user.getId());
??}
?}
?//外聯(lián)查詢(xún)中一定要添加fetch關(guān)鍵字否則生產(chǎn)的被動(dòng)關(guān)聯(lián)對(duì)象不會(huì)填充到主動(dòng)關(guān)聯(lián)對(duì)象的set中,只能
?//在返回的結(jié)果集中作為單獨(dú)的對(duì)象存在,這樣就不能通過(guò)主動(dòng)對(duì)象訪問(wèn)到其關(guān)聯(lián)的被動(dòng)對(duì)象。
?//外聯(lián)返回的list中包含條目是按照數(shù)據(jù)庫(kù)中生成的記錄數(shù)目相對(duì)應(yīng)的,所有記錄數(shù)生成對(duì)應(yīng)的對(duì)象。
?public void testOutjoin(){
??TUser user=null;
??TAddress address=null;
??String hq1="from? TUser user left join fetch user.TAddresses";
??Query query=session.createQuery(hq1);
??List list=query.list();
??Iterator it=list.iterator();
??Iterator addit=null;
??while(it.hasNext()){
???user=(TUser)it.next();
???System.out.println(user.getName());
???System.out.println("=============");
???addit=user.getTAddresses().iterator();
???while(addit.hasNext()){
????address=(TAddress)addit.next();
????System.out.println(address.getAddress());
????
???}
???
???
??}
?}
?//不使用fetch關(guān)鍵字時(shí)返回的list中 的條目是TUser類(lèi)對(duì)象和與其關(guān)聯(lián)的被控對(duì)象TAddress類(lèi)對(duì)象組成的object[]對(duì)象數(shù)組。
?public void testOutFetch(){
??TUser user=null;
??TAddress address=null;
??String hq1="from? TUser user left join? user.TAddresses";
??Query query=session.createQuery(hq1);
??List list=query.list();
??Iterator it=list.iterator();
??Iterator addit=null;
??while(it.hasNext()){
???Object[]result=(Object[])it.next();
???for(int i=0;i<result.length;i++){
????System.out.println(result[i]);
???}
???
???
???
??}
?}
?//測(cè)試子查詢(xún),hql中的子查詢(xún)區(qū)別于sql中的子查詢(xún),如下所示為相關(guān)子查詢(xún)。使用中要注意區(qū)別相關(guān),非相關(guān)子查詢(xún)。
?public void testSub(){
??String hql="from TUser user where (select count(*) from user.TAddresses)>1";
??Query query=session.createQuery(hql);
??List list=query.list();
??Iterator it=list.iterator();
??while(it.hasNext()){
???TUser user=(TUser)it.next();
???System.out.println(user.getName());
??}
?}
}
posted on 2013-03-14 14:49 moonfans 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/moonfans/archive/2013/03/14/2959349.html
總結(jié)
- 上一篇: WordPress Terillion
- 下一篇: centos eclipse 安装