java JPA:我应该使用哪个?基本(可选)或列(可为空)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5602908/
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
JPA: Which should I use? Basic(optional) or Column(nullable)?
提问by Xiè Jìléi
For simple string field,
对于简单的字符串字段,
@Entity
class Foo {
//1. @Basic(optional = false)
//2. @Column(length = 100, nullable = false)
String name;
}
I need to restrict name's length using @Column
annotation, but I'm confused with the nullable attribute. While I'm using other annotations like @ManyToOne
and @OneToMany
those use optional
attributes, I feel like to use @Basic(optional)
to keep most annotations uniform. But I can't restrict the name's length with @Basic
.
我需要使用@Column
注释来限制名称的长度,但我对可空属性感到困惑。虽然我正在使用其他注释@ManyToOne
和@OneToMany
那些使用optional
属性的注释,但我想使用它@Basic(optional)
来保持大多数注释的统一。但我不能用@Basic
.
So, where should I annotate the nullable attribute, by @Basic
or @Column
?
那么,我应该在哪里注释可空属性,通过@Basic
或@Column
?
EDIT
编辑
Simply say, in which form would you prefer:
简单地说,您更喜欢哪种形式:
Form 1:
表格一:
@Entity
class Foo {
@Basic(optional = false)
@Column(length = 100)
String name;
}
Form 2:
表格2:
@Entity
class Foo {
@Column(length = 100, nullable = false)
String name;
}
Well personally I like Form 1, because optional
attribute is also used by @ManyToOne
etc. annotations, but Form 2 is also good because it's done in single annotation.
嗯,我个人喜欢 Form 1,因为optional
属性也被@ManyToOne
etc. annotations 使用,但是 Form 2 也很好,因为它是在单个 annotation 中完成的。
EDIT
编辑
After read http://markmail.org/message/osod6rsauwbnkvya, I've got the difference between @Basic.optional
and @Column.nullable
. But I still don't know which one I should use. It seems like good to include both annotations, so make the underlying table well defined, and check null in JPA before the actual update maybe slightly faster.
阅读http://markmail.org/message/osod6rsauwbnkvya 后,我知道@Basic.optional
和之间的区别@Column.nullable
。但我仍然不知道我应该使用哪一个。包含两个注释似乎很好,因此请明确定义基础表,并在实际更新之前检查 JPA 中的 null 可能会稍微快一些。
采纳答案by Reddy
From API documentation:
来自 API 文档:
@Basic annotation is the simplest type of mapping to a database column. The Basic annotation can be applied to a persistent property or instance variable of any of the following types: Java primitive types, wrappers of the primitive types, String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements Serializable.
@Basic 注释是映射到数据库列的最简单类型。Basic 注释可以应用于以下任何类型的持久属性或实例变量:Java 原始类型、原始类型的包装器、String、java.math.BigInteger、java.math.BigDecimal、java.util.Date、 java.util.Calendar、java.sql.Date、java.sql.Time、java.sql.Timestamp、byte[]、Byte[]、char[]、Character[]、枚举和任何其他实现可序列化的类型。
@Column Is used to specify a mapped column for a persistent property or field. If no Column annotation is specified, the default values are applied.
@Column 用于为持久属性或字段指定映射列。如果未指定 Column 注释,则应用默认值。
So, if you don't specify @Column
it derives column value from getter/setter.
If you need to specify column name you have to @Column
annotation.
因此,如果您不指定@Column
它从 getter/setter 派生列值。如果需要指定列名,则必须进行@Column
注释。
@Basic
allows you to specify Fetch Type. If you want to change default fetching type you have to use this annotation, otherwise you can omit it.
@Basic
允许您指定提取类型。如果要更改默认获取类型,则必须使用此注释,否则可以省略它。