ssh 中查询数据库老报错 帮帮忙谢谢了
这是我查询的dao
public class AdminDaoImpl implements AdminDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Admin findByUsername(final String username){
HibernateTemplate hibernateTemplate = new HibernateTemplate
(this.sessionFactory);
return hibernateTemplate.execute(
new HibernateCallback<Admin>(){
@Override
public Admin doInHibernate(Session session)
throws
HibernateException,
SQLException {
Admin admin;
System.out.println
("username"+username);
Transaction ts =
session.beginTransaction();
String sql = "from Admin a where
a.username = '" + username + "'";
Query y = session.createSQLQuery
(sql);
List list = y.list();
System.out.println("ss"+list.size
());
admin = (Admin)list.get(0);
ts.commit();
return admin;
}
}
);
}
查询的时候 报这些错误:
org.springframework.dao.InvalidDataAccessResourceUsageException:
could not execute query;
SQL [from Admin a where a.username = '111'];
nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException
(SessionFactoryUtils.java:635)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException
(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute
(HibernateTemplate.java:411)
org.springframework.orm.hibernate3.HibernateTemplate.execute
(HibernateTemplate.java:339)
wang.test.DAO.AdminDaoImpl.findByUsername(AdminDaoImpl.java:26)
wang.test.SERVICE.AdminManagerImpl.findByUsername(AdminManagerImpl.java:14)
wang.test.ACTION.AdminLogin.juageLogin(AdminLogin.java:48)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
而且我用getHibernateTemplate.get()查询老报
空指针错误 。。我确认了 getHibernateHibrenate得到
的不是空 但是get()确为空
------解决方案--------------------
String sql = "from Admin where username = '" + username + "'";
Query y = session.createQuery(sql);
楼主使用的是Query orm 实体查询
就不要使用sqlQuery 原生态sql
试试、不行的话把全部错误信息贴出来看看、
------解决方案--------------------
Query y = session.createSQLQuery;貌似这个应该用createQuery(sql),我半年没有摸java的不知道是不是这样啊 ,一个hql一个是sql,楼主写的是hql应该用createQuery吧 ??
------解决方案--------------------
Query y = session.createSQLQuery(sql); 换成Query y = session.createQuery(sql);
------解