Java 如何在 Hibernate 查询中将整数参数设置为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25667914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:53:03  来源:igfitidea点击:

How to set Integer parameter as null in Hibernate query?

javahibernate

提问by Tony

I've got an entity:

我有一个实体:

    @Entity
    @Table(name = "smsc.security_users")
    public class User {

        @Id
        @Column(name = "id")
        private int id;

        @Column(name = "username")
        private String username;

        @Column(name = "password")
        private String password;

        @Column(name = "enabled")
        private int enabled;

        @Column(name = "master_id", nullable = true)
        private Integer master_id;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "passwordExpiration")
        private Date passwordExpiration;

        public String getUsername() {
            return username;
        }
        ...

I'm talking about master_id. And this is my DAO method:

我在谈论master_id. 这是我的 DAO 方法:

public void add(String userName, String password, Integer master_id) {
        Session session = null;
        session = this.sessionFactory.getCurrentSession();
        Query query = session
                .createSQLQuery(
                        "INSERT INTO smsc.security_users(id,username,password,enabled,passwordExpiration,master_id) VALUES(smsc.SeqDictID.nextval+1,:userName,:password,1,:passwordExpiration,:master_id)")
                .setString("userName", userName)
                .setString("password", Sha1.getHash(password))
                .setTimestamp("passwordExpiration", DateUtil.getTomorrow())
                .setInteger("master_id", master_id);
        int updated = query.executeUpdate();

    }

Log says:

日志说:

Request processing failed; nested exception is java.lang.NullPointerException

请求处理失败;嵌套异常是 java.lang.NullPointerException

and puts pointer at this line:

并将指针指向这一行:

.setInteger("master_id", master_id);

master_idmust be null sometimes. So how can I set null?

master_id有时必须为空。那么如何设置空值呢?

采纳答案by BatScream

If you check the documentation of the Query class, setInteger() method,

如果您查看 Query 类的文档, setInteger() 方法,

Query setInteger(String name,int val)

It takes a name and a primitive type int, as parameters.

它接受一个名称和一个原始类型 int作为参数。

When you pass a wrapper type Integer, whose value is null, during autoboxing, a null pointer Exception occurs.

当您传递值为 null 的包装类型 Integer 时,在自动装箱期间,会发生空指针异常。

For example:

例如:

Integer x = null;
int y = x;  // gives a nullpointer exception.

You could use

你可以用

Query setParameter(String name,
                   Object val,
                   Type type);

method to set null values.

设置空值的方法。

See Also:

也可以看看:

Hibernate: How to set NULL query-parameter value with HQL?

Hibernate:如何使用 HQL 设置 NULL 查询参数值?

回答by Roger

For a null object, specify it as:

对于空对象,将其指定为:

query.setParameter("name", object, StandardBasicTypes.INTEGER);

StandardBasicTypes.INTEGERreplaced Hibernate.INTEGER(etc)

StandardBasicTypes.INTEGER替换Hibernate.INTEGER(等)