日期:2014-05-19  浏览次数:20691 次

hibernate中关于HQL小问题请教下大家,谢谢
写了一个测试程序,在执行到query = session.createQuery(hql); 发生异常,没报错,但跳转到HibernateTemplate不相关的类。
如果将hql改为String hql = " from User a where a.name =1";就能正常执行,为什么啊?纠结了半天,请教下大家,谢谢
  分数不够了,谢谢大家
 public Object doInHibernate(Session session)
  throws HibernateException, SQLException
  {
  String hql = " from User a where a.name =:name";
  Query query = null;
  try
  {
  query = session.createQuery(hql);
  }
  catch (Exception e)
  {
  e.printStackTrace();
  }
   
  return query.list();
  }
  }

------解决方案--------------------
query = session.createQuery(hql);
query.setString("name", name);
------解决方案--------------------
String hql = " from User a where a.name =:name";这样写都有问题,你如果要传值就要用 String hql = " from User a where a.name = "+"1";或者是 String hql = " from User a where a.name ="+name;然后给name变量赋值才对啊
------解决方案--------------------
String hql = " from User a where a.name =?";
Query query = session.createQuery(hql);
query.setString(0, name);
------解决方案--------------------
你的name只是个键这个键没有设置值,
query.setParameter("name","name");OK