25

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.

slartidan
  • 20,403
  • 15
  • 83
  • 131
Tahir
  • 3,344
  • 14
  • 51
  • 69

2 Answers2

53

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;
Henning
  • 16,063
  • 3
  • 51
  • 65
7

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 !
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
coldiron
  • 101
  • 1
  • 2