21

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:

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

and puts pointer at this line:

.setInteger("master_id", master_id);

master_id must be null sometimes. So how can I set null?

8
  • 1
    Try: .setParameter("master_id", master_id) Commented Sep 4, 2014 at 14:28
  • @DmitryTsechoev, ora-00932 inconsistent datatypes expected number got binary Commented Sep 4, 2014 at 14:31
  • a workaround for this would be to create 2 separate methods and specify null inside SQL query string Commented Sep 4, 2014 at 14:38
  • Maybe it will help (look at answer): stackoverflow.com/questions/14077721/… Commented Sep 4, 2014 at 14:40
  • @Funtik, ok. But I thought we can put a java reference as parameter that might be null. Commented Sep 4, 2014 at 14:41

2 Answers 2

26

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

Query setInteger(String name,int val)

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

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

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Well, I know that part with null. My Integer migth be null. That's why I chose wrapper as it's type.
Did you try the setParameter() with the three arguments? According to the document it should definitely work.
What type it should be?
Can you try IntegerType.INSTANCE, i guess Hibernate.INTEGER is deprecated. Have updated my answer with a link, that's just a workaround though.
13

For a null object, specify it as:

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

StandardBasicTypes.INTEGER replaced Hibernate.INTEGER (etc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.