hql语句查询参数数组的问题

chen2713756 2014-04-02 05:50:38
public long getTotalUser(long userID)
{
String hql = "from User t where t.userId =? and t.userName is not null" ;

return find(hql,new Object[]{userID}).size();
}

这个方法在查询的时候,查询结果会报错:
com.sun.jdi.InvalidTypeException: Generated value (long) is not compatible with declared type (java.lang.Object). occurred while setting value in array.

把数组修改成new long[]{userID}后,同样报错:
An exception occurred: java.lang.ClassCastException

方法修改成如下,方法可以正常执行,但是这样的写法可以防sql注入吗?如果不能,上面的方法中参数该怎么处理?
public long getTotalUser(long userID)
{
String hql = "from User t where t.userId = " + userID + " and t.userName is not null" ;

return find(hql).size();
}
...全文
234 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Spring89 2014-04-02
  • 打赏
  • 举报
回复
不要用createQuery,不要用find,find只是用来做简单的查询!

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧