hql多表聯合查詢


HQL多表聯合查詢的問題       HQL查詢多表的時候,取出結果是兩個對象的列表,但是我只要我自己想要的屬性,之前的HQL語句是這樣寫的:

from  Hytxbz h,Tgbzk t  where  h.hytxbzid = t.hytxbzid  and  t.bztgid = :bztgid

結果我debug去看query.list();是Hytxbz和Tgbzk兩個對象的列表,結果並不是我想要的,我改成

from  Hybztx h  where  h.hytxbzid  in  ( select  t.hytxbzid  from  Tgbzk  where  t.bztgid  = :bztgid)

還是不行,google一把,發現可以這樣寫

select  h  from  Hytxbz  as  h,Tgbzk  as  t  where  h.hytxbzid = t.hytxbzid  and  t.bztgid = :bztgid

如果想取得對應屬性的話,也可以這樣寫 

select  h.hytxbzid  from  Hytxbz  as  h,Tgbzk  as  t  where  h.hytxbzid = t.hytxbzid  and  t.bztgid = :bztgid

發現in的語句可以這樣寫

String   ids[] = new    String[] {"1","2","3"} ;      
  String   hql =     "    from   com,you.YourPOJO   where   id   in   (?) " ;           
  Query   query    =    session.createQuery(hql);      
query   .setParameters(ids);           
相關方法:            
 Query   setParameters(Object[]   objectArray,   Type[]   typeArray)    throws    HibernateException;          
Query   setParameterList(String   string,   Collection   collection,   Type   type)    throws    HibernateException;         
 Query   setParameterList(String   string,   Collection   collection)    throws    HibernateException;          
Query   setParameterList(String   string,   Object[]   objectArray,   Type   type)    throws    HibernateException;         
 Query   setParameterList(String   string,   Object[]   objectArray)    throws    HibernateException


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM