Java 如何在 JPA - GAE/J 中使属性可以为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1910215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a property nullable in JPA - GAE/J?
提问by Tahir Akram
I have a entity class User
. I want to add some more properties but to keep them nullable.
我有一个实体类User
。我想添加更多属性,但要保持它们可为空。
What is the annotation used for this in JPA?
JPA 中用于此的注释是什么?
I am using JPA in Google App Engine.
我在 Google App Engine 中使用 JPA。
采纳答案by Henning
Properties are nullable by default in JPA, except primitive types. You can control nullability using the nullable
property of the @Column annotation, like so:
默认情况下,JPA 中的属性可为空,原始类型除外。您可以使用nullable
@Column 注释的属性控制可空性,如下所示:
//not nullable
@Column(nullable = false)
private String prop1;
//nullable
@Column(nullable = true)
private String prop2;
//default = nullable
@Column
private String prop3;
回答by coldiron
In your Entity class use Integer, Double, rather than int and double.
在您的实体类中使用 Integer、Double,而不是 int 和 double。
@Entity
public class AnyTable {
...
public double myValue; // !! dont use in this way - this can never be null!
public Double myValue; // BETTER !