hibernate query java.lang.NullPointerException问题 10
publicListfindOpenByUploader(Stringuploader){try{Queryquery=getSession().createQuery(...
public List findOpenByUploader(String uploader){
try {
Query query = getSession().createQuery("from ProblemStatus s where s.defunct =:defunct and s.uploader =:uploader order by s.id");
query.setString("uploader", uploader);
query.setParameter("defunct", "N");
return query.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
抛出错误的语句是return query.list();,抛出java.lang.NullPointerException
问题在query.setString("uploader", uploader);中,
当uploader为字母开头的字符串时,不会抛出错误信息,如uploader为“root”
但当uploader为数字开头的字符串时,死活都抛出如上错误信息,如uploader为“012345”
query.setString("uploader", uploader);改为下面的任意一种都一样抛出错误信息
query.setString("uploader", ""+uploader);
query.setParameter("uploader", ""+uploader);
query.setParameter("uploader", uploader);
query.setParameter("uploader", uploader,Hibernate.STRING);
请高手指点
非常skshinji感谢!
按照您的方法修改,或抛出无改参数的错误
java.lang.IllegalArgumentException: Parameter uploader does not exist as a named parameter in [from ProblemStatus s where s.defunct =:defunct and s.uploader =':uploader' order by s.id] 展开
try {
Query query = getSession().createQuery("from ProblemStatus s where s.defunct =:defunct and s.uploader =:uploader order by s.id");
query.setString("uploader", uploader);
query.setParameter("defunct", "N");
return query.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
抛出错误的语句是return query.list();,抛出java.lang.NullPointerException
问题在query.setString("uploader", uploader);中,
当uploader为字母开头的字符串时,不会抛出错误信息,如uploader为“root”
但当uploader为数字开头的字符串时,死活都抛出如上错误信息,如uploader为“012345”
query.setString("uploader", uploader);改为下面的任意一种都一样抛出错误信息
query.setString("uploader", ""+uploader);
query.setParameter("uploader", ""+uploader);
query.setParameter("uploader", uploader);
query.setParameter("uploader", uploader,Hibernate.STRING);
请高手指点
非常skshinji感谢!
按照您的方法修改,或抛出无改参数的错误
java.lang.IllegalArgumentException: Parameter uploader does not exist as a named parameter in [from ProblemStatus s where s.defunct =:defunct and s.uploader =':uploader' order by s.id] 展开
3个回答
展开全部
from ProblemStatus s where s.defunct =:defunct and s.uploader =:uploader order by s.id
ProblemStatus s 自动生成的中间有个as
如 from ProblemStatus as s where s.defunct =:defunct and s.uploader =:uploader order by s.id
至于Query 的setString 方法对于数字应该没什么问题除了你的Hibernate包有问题,或者配置地方弄错了
ProblemStatus s 自动生成的中间有个as
如 from ProblemStatus as s where s.defunct =:defunct and s.uploader =:uploader order by s.id
至于Query 的setString 方法对于数字应该没什么问题除了你的Hibernate包有问题,或者配置地方弄错了
展开全部
Query query = getSession().createQuery("from ProblemStatus s where s.defunct =:defunct and s.uploader =':uploader' order by s.id");
把sql改成这样试试是不是可以。看你的样子是把字符串型的误认为是数字了
把sql改成这样试试是不是可以。看你的样子是把字符串型的误认为是数字了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码太少,异常太少。估计是(List)all这里为空,很有可能是super.getSession()并未拿到Session,可以分两步把这里的代码改写一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询