当前位置: 代码迷 >> J2EE >> hql 语句 带 in 的子查询,sql语句测试没有有关问题,但是hql语句查不出数据来
  详细解决方案

hql 语句 带 in 的子查询,sql语句测试没有有关问题,但是hql语句查不出数据来

热度:530   发布时间:2016-04-22 03:22:45.0
hql 语句 带 in 的子查询,sql语句测试没有问题,但是hql语句查不出数据来。
public List<Users> getcontactList(Integer userId) throws Exception{
List<Users> list = null;
  //通过联系人表 Contacts 里面的userId 查询 USers表里面的用户信息。
String hql ="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();
}
}
拜托大家了。

------解决方案--------------------
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(); 



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