日期:2014-05-16  浏览次数:20581 次

Hibernate 查询的区别

//从工厂获取的session
Session session = HibernateSessionFactory.getSession();//org.hibernate.Session
String hql = "from Erpuser as model where model.userName=:userName"+" and model.userPwd=:userPwd";
Query query = session.createQuery(hql);
query.setParameter("userName", username);
query.setParameter("userPwd", pwd);
return query.list();

//默认使用父类中获取session的方法
String hql = "select * from Erpuser as model where model.userName=:userName"+" and model.userPwd=:userPwd";//不加select会报from语法错误
SQLQuery query = getSession().createSQLQuery(hql);//org.hibernate.Session
query.setParameter("userName", username);
query.setParameter("userPwd", pwd);
return query.addEntity(Erpuser.class).list();//不加addEntity会报Mapping映射错误可在此处修改映射关系,比如viewbean
?