在hibernate中,用hql語(yǔ)句查詢(xún)實(shí)體類(lèi),采用list方法的返回結(jié)果為一個(gè)List,該List中封裝的對(duì)象分為以下三種情況:
1.查詢(xún)?nèi)孔侄蔚那闆r下,如"from 實(shí)體類(lèi)",list中封裝的對(duì)象為實(shí)體類(lèi)本身,各屬性都將得到填充。
2.只查詢(xún)一個(gè)字段,默認(rèn)情況下,list中封裝的是Object對(duì)象。
3.查詢(xún)兩個(gè)或兩個(gè)以上的字段,默認(rèn)情況下,list中封裝的是Object[],長(zhǎng)度與所查詢(xún)的字段數(shù)一致。
1.查詢(xún)?nèi)孔侄蔚那闆r下,如"from 實(shí)體類(lèi)",list中封裝的對(duì)象為實(shí)體類(lèi)本身,各屬性都將得到填充。
2.只查詢(xún)一個(gè)字段,默認(rèn)情況下,list中封裝的是Object對(duì)象。
3.查詢(xún)兩個(gè)或兩個(gè)以上的字段,默認(rèn)情況下,list中封裝的是Object[],長(zhǎng)度與所查詢(xún)的字段數(shù)一致。
對(duì)于后兩種情況,用標(biāo)簽遍歷時(shí)不太方便,因?yàn)闊o(wú)法直接轉(zhuǎn)換成實(shí)體類(lèi)的對(duì)象。比較簡(jiǎn)單的解決方法是:
の:在hql中使用select new 包名.類(lèi)名(屬性1,屬性2……) from 實(shí)體類(lèi),同時(shí)在實(shí)體類(lèi)中添加帶參的構(gòu)造方法,參數(shù)的個(gè)數(shù)和順序與(屬性1,屬性2……) 保持一致,這樣我們得到的list中存放的依然是實(shí)體類(lèi)的對(duì)象,所查詢(xún)到的屬性得到了填充,使用起來(lái)更為方便。の:hql查詢(xún)多表部分字段,select new 包名.表1實(shí)體類(lèi)名(表1.屬性1,表2.屬性2……) from 表1實(shí)體類(lèi),表2實(shí)體類(lèi) where 表1.ID=表2.ID(即相關(guān)聯(lián)的字段),同時(shí)在要返回的表1實(shí)體類(lèi)中添加表2的屬性和帶參的構(gòu)造方法,參數(shù)的個(gè)數(shù)和順序與(表1.屬性1,表2.屬性 2……) 保持一致
例如要查詢(xún)Problem 中的pid,score,title,totalAccept,totalSubmission,unSee
public class Problem {
private int pid;
private int score;
private int timeLimit;
private int memoryLimit;
private int totalAccept;
private int totalSubmission;
private int unSee;
private String title;
private String description;
private String input;
private String output;
public Problem(int pid, int score,String title, int totalAccept, int totalSubmission,
int unSee) {
super();
this.pid = pid;
this.score = score;
this.totalAccept = totalAccept;
this.totalSubmission = totalSubmission;
this.unSee = unSee;
this.title = title;
}
//省略getter 和 setter
}
查詢(xún)語(yǔ)句如下
Query query=session.createQuery("select new Problem(pid,score,title,totalAccept,totalSubmission,unSee) from Problem order by pid");
//query.setFirstResult(firstResult); //分頁(yè)函數(shù)
//query.setMaxResults(maxResutl);
List<Problem> problems=query.list();//返回的還是Problem對(duì)象
關(guān)于hibernate的問(wèn)題:
我現(xiàn)在有條
hql="select s.id,s.name,t.id,t.name from User s,Useraddress t where t.id=s.id"
這條sql里面的User和Useraddress是兩個(gè)實(shí)體類(lèi),現(xiàn)在組合查詢(xún)分別取出來(lái)兩個(gè)實(shí)體類(lèi)里面的兩個(gè)字段,然后我想再建立一個(gè)實(shí)體類(lèi)Result,里面定義這四個(gè)結(jié)果集里面的字段,能不能執(zhí)行完這條hql,正好把這個(gè)結(jié)果集對(duì)應(yīng)到實(shí)體類(lèi)Result里面呢,Result這個(gè)實(shí)體類(lèi),沒(méi)寫(xiě)映射文件Result.hbm.xml.
希望能幫下忙
2種做法
創(chuàng)建一個(gè)class temp
有屬性sid,name,tid,sname,tname
創(chuàng)建一個(gè)構(gòu)造函數(shù)
public temp(sid,name,tid,sname,tname)
{
}
1.hql中
List<temp>
select new temp(s.id,s.name,t.id,t.name) from User s,Useraddress t where t.id=s.id
2.List
記錄的每一行是object[] 遍歷
object[0] ==s.id
object[1] ==s.name
object[2] ==t.id
object[3] ==t.name
關(guān)于hibernate的問(wèn)題:
我現(xiàn)在有條
hql="select s.id,s.name,t.id,t.name from User s,Useraddress t where t.id=s.id"
這條sql里面的User和Useraddress是兩個(gè)實(shí)體類(lèi),現(xiàn)在組合查詢(xún)分別取出來(lái)兩個(gè)實(shí)體類(lèi)里面的兩個(gè)字段,然后我想再建立一個(gè)實(shí)體類(lèi)Result,里面定義這四個(gè)結(jié)果集里面的字段,能不能執(zhí)行完這條hql,正好把這個(gè)結(jié)果集對(duì)應(yīng)到實(shí)體類(lèi)Result里面呢,Result這個(gè)實(shí)體類(lèi),沒(méi)寫(xiě)映射文件Result.hbm.xml.
希望能幫下忙
2種做法
創(chuàng)建一個(gè)class temp
有屬性sid,name,tid,sname,tname
創(chuàng)建一個(gè)構(gòu)造函數(shù)
public temp(sid,name,tid,sname,tname)
{
}
1.hql中
List<temp>
select new temp(s.id,s.name,t.id,t.name) from User s,Useraddress t where t.id=s.id
2.List
記錄的每一行是object[] 遍歷
object[0] ==s.id
object[1] ==s.name
object[2] ==t.id
object[3] ==t.name
感謝glamey兄弟的文章,正好解決了當(dāng)前遇到的問(wèn)題。原文鏈接如下:http://glamey.iteye.com/blog/721019
假設(shè)我們現(xiàn)在有一個(gè)DTO,其屬性包括兩張表的屬性,我們現(xiàn)在需要將sql語(yǔ)句查詢(xún)得到的內(nèi)容轉(zhuǎn)為一個(gè)DTO對(duì)象,其解決方法如下:
String sql = "select u.userName as userName ,p.title as title ,p.addTime as addTime from user as u,post as p where u.id=p.userId"
Query q = factory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(PostVO.class));
上面select中as后面的內(nèi)容必須和PostVO中屬性名一致,這樣就可以返回一個(gè)針對(duì)PostVO的一個(gè)集合。
其實(shí)大家可以看下hibernate這一部分的源碼就會(huì)發(fā)現(xiàn),主要是使用了AliasToBeanResultTransformer這個(gè)類(lèi),通過(guò)sql的查詢(xún),會(huì)返回?cái)?shù)組,然后hibernate根據(jù)數(shù)據(jù)表的映射,自動(dòng)幫我們來(lái)set對(duì)應(yīng)的字段屬性,所以標(biāo)紅的部分務(wù)必要跟VO中的屬性值一直,要不然會(huì)報(bào)錯(cuò)的。
如果需要的話,大家也可以重寫(xiě)這個(gè)類(lèi)。例如VOResultTransformer。然后在dao中更改成:
另外,除了以上glamey的方法外,還有一種方法:
其中,MsgInfo是DTO。值得注意的是,第二種方法中DTO必須提供帶參數(shù)的構(gòu)造方法,并且HQL語(yǔ)句中屬性的位置要與構(gòu)造方法中的位置一一對(duì)應(yīng)。
上面select中as后面的內(nèi)容必須和PostVO中屬性名一致,這樣就可以返回一個(gè)針對(duì)PostVO的一個(gè)集合。
其實(shí)大家可以看下hibernate這一部分的源碼就會(huì)發(fā)現(xiàn),主要是使用了AliasToBeanResultTransformer這個(gè)類(lèi),通過(guò)sql的查詢(xún),會(huì)返回?cái)?shù)組,然后hibernate根據(jù)數(shù)據(jù)表的映射,自動(dòng)幫我們來(lái)set對(duì)應(yīng)的字段屬性,所以標(biāo)紅的部分務(wù)必要跟VO中的屬性值一直,要不然會(huì)報(bào)錯(cuò)的。
如果需要的話,大家也可以重寫(xiě)這個(gè)類(lèi)。例如VOResultTransformer。然后在dao中更改成:
setResultTransformer(new VOResultTransformer(PostVO.class));
另外,除了以上glamey的方法外,還有一種方法:
Query q = session.createQuery("select new com.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg m");
List<MsgInfo> list=q.list();
其中,MsgInfo是DTO。值得注意的是,第二種方法中DTO必須提供帶參數(shù)的構(gòu)造方法,并且HQL語(yǔ)句中屬性的位置要與構(gòu)造方法中的位置一一對(duì)應(yīng)。