1.在根据ID查询用户信息时报了如下错误,
java.lang.NullPointerException
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:364)
at org.hibernate.hql.internal.ast.tree.IdentNode.getDataType(IdentNode.java:319)
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:152)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:859)
后来根据分析错误原因是hql写的不对,
错误的写法是:
public User getUser(String id) {
String hql = "select User from User u where u.id = ?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, id);
User user = (User) query.uniqueResult();
return user;
}
正确的写法是:
public User getUser(String id) {
String hql = "from User u where u.id = ?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, id);
User user = (User) query.uniqueResult();
return user;
}
hql中加上"select User"是错误的原因。删掉以后就没问题了。