在线等关于MyEclipse+struts+Hibernate查询SQL错误,那位达人告诉下?急急急

来源:百度知道 编辑:UC知道 时间:2024/06/01 01:45:24
public class UserinfoDaoImpl {
public boolean dbcon(String a, String b) {

try {

Session sess=HibernateSessionFactory.currentSession();

String sql = "select * from Userinfo as u where u.loginname='"+a+"' and u.password='"+b+"'";

System.out.println(sql);

Query q=sess.createSQLQuery(sql);
List list=q.list();

System.out.println(list.toString());

return false;

} catch (Exception e) {
System.out.println(e);
return false;
} finally {

}

}
public static void main(String args[]){
UserinfoDaoImpl dt=new UserinfoDaoImpl ();
boolean flag=dt.dbcon("user","user");

System.out.println(flag);

}
}
错误如下:
org.hibernate.QueryException:addEntity() or addScalar() must be called on a sql query before executing th

试下这个 改了两句
String sql = "from Userinfo as u where u.loginname='"+a+"' and u.password='"+b+"'";

Query q=sess.createQuery(sql);

另外最好关闭session 不然再次检索会报错

SessionFactory.closeSession();
finally {
if (session != null) {
try {
session.close();
} catch (HibernateException e) {
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}

就象楼上所说的
你既然使用了hibernate你为什么不用HQL语言呢
再者你这个返回一个UserInfo表的PO对象不就行了,还用个LIST,你不觉的这样很浪费吗??