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?
You have to use is null and is not null in HQL.
The JPA specification defines that during ordering, NULL values shall be handled in the same way as determined by the SQL standard. The standard specifies that all null values shall be returned before or after all non-null values. It's up to the database to pick one of the two options.
Following example shows how to use IS NULL to find properties values which have not been set.
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?
For a null object, specify it as:
query.setParameter("name", object, StandardBasicTypes.INTEGER);
StandardBasicTypes.INTEGER
replaced Hibernate.INTEGER
(etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With