74H1R
74H1R

Reputation: 3514

How to make a property nullable in JPA - GAE/J?

I have a entity class User. I want to add some more properties but to keep them nullable.

What is the annotation used for this in JPA?

I am using JPA in Google App Engine.

Upvotes: 27

Views: 55345

Answers (2)

coldiron
coldiron

Reputation: 111

In your Entity class use Integer, Double, rather than int and double.

@Entity
public class AnyTable {
  ...
  public double myValue; // !! dont use in this way - this can never be null!

  public Double myValue; // BETTER !

Upvotes: 8

Henning
Henning

Reputation: 16311

Properties are nullable by default in JPA, except primitive types. You can control nullability using the nullable property of the @Column annotation, like so:

//not nullable
@Column(nullable = false)
private String prop1;

//nullable
@Column(nullable = true)
private String prop2;

//default = nullable
@Column
private String prop3;

Upvotes: 56

Related Questions