public List <Users> getcontactList(Integer userId) throws Exception{ 
List <Users> list = null; 
                            //通过联系人表 Contacts 里面的userId 查询 USers表里面的用户信息。 
String hql ="select * from Users u where u.userId in (SELECT c.friendId FROM Contacts c where c.userId =:contactuserId)"; 
Session session = null; 
try 

session = super.getSessionFactory().openSession(); 
Query query = session.createQuery(hql); 
query.setInteger("contactuserId",userId); 
//query.setParameters();  
list = query.list(); return list; 

catch (Exception ex) 

throw new HibernateException(ex); 

finally 

if (session != null) 

session.close(); 


解决方案 »

  1.   

    你确定没有查询出数据吗?你有做断点看看list到底有没有数据?你应该在return list; 的地方打断点看看,然后看看输出的sql对不对
      

  2.   

    我在 return list 的时候,看过断点 ,是 空的。
      

  3.   

    我怀疑是应该用 
    query.setParameters();  
    但是我是一个子语句,我不清楚该如何用?这个设置?
     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; 
      

  4.   

    你确定hql可以这样写吗?  我今天也要用到这样的语句 - -   就是不确定到底可不可以....
    hql="select * from SalaryBaseDataForm s  where 1=1 and s.cardId in (select u.cardId form SalaryUserForm u where u.depertment='"+suf.getDepertment()+"')";