-------------------------- 題記
1.若有關係,班級1-n學生,學生1-n衣服,在hbm配置文件中,都使用延遲加載,那麼查班級,如何使用hql同時預加載學生和衣服呢?
2.hql中使用select 挑選多個屬性/對象,如何分別取得其值?
看代碼。。
-------------------------- 代碼
code1:
//必須加上distinct
new String('select distinct s '+
'from Student s '+
'join fetch s.myClothes');
new String('select distinct c '+
'from MyClass c '+
'join fetch c.students s '+
'join fetch s.myClothes');
code2:
List<Object[]> list = getHibernateTemplate().find(
"select c,c.className from MyClass c");
for (Object[] objs : list) {
System.out.println(objs[0]);
System.out.println(objs[1]);
}
--------------------------- 注
如上code1中,挑選的對象必須加上distinct修飾,挑選出無重複的,因爲使用預加載join fetch會有很多重複,具體原因也沒仔細研究。
而code2中,經過hibernate封裝出來的對象是List<Object[]>(即集合中存儲了數組的引用),每個集合的元素(element)便是一個數組的引用,使用該引用(objs[0])便可以得到hql中select挑選出來的屬性/對象了。