Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 691
Tahir Avatar asked Dec 15 '09 20:12

Tahir


People also ask

How do you make a field nullable in Java?

A field can be set to null just by creating an empty Value object. Here is the snippet which would help. Field field = arserveruser. getField("FORM_NAME", 536870915, null);

How does JPA handle null values?

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.

Does JPA repository return null?

The normal behavior is indeed returning an empty list if no results are found. If a List<Object> is the return value of the method in the defined interface, the method should never return Null .

How do you make a column NOT NULL in JPA?

@Column(nullable = false) is the JPA way of declaring a column to be not-null. I.e. the former is intended for validation and the latter for indicating database schema details.


2 Answers

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; 
like image 118
Henning Avatar answered Nov 03 '22 09:11

Henning


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 ! 
like image 45
coldiron Avatar answered Nov 03 '22 09:11

coldiron