将变量传递给休眠标准

时间:2011-08-26 07:57:52

标签: java hibernate

Criteria crt = hibernateSession.createCriteria(Post.class);  crt.add(Restrictions.eq("LogId", new Integer(1234)));

上面的查询完全没有问题。但是如果将“new Integer(1234)”更改为如下变量,则查询将返回java.lang.NullPointerException错误。

int test = 1234;
Criteria crt = hibernateSession.createCriteria(Post.class); crt.add(Restrictions.eq("LogId", test));

有人知道这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

最可能的解释是您正在使用java 1.4(或更早版本)运行时,它运行 not autobox原语。

Restrictions.eq()接受Object是它的第二个参数,int将在java 1.4之后自动装箱到Integer个对象中。