Java 持久性 / JPA:@Column 与 @Basic
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1383229/
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
Java Persistence / JPA: @Column vs @Basic
提问by Hosam Aly
What is the difference between @Column
and @Basic
annotations in JPA? Can they be used together? Shouldthey be used together? Or does one of them suffice?
JPA中的注解@Column
和@Basic
注解有什么区别?它们可以一起使用吗?如果他们可以一起使用吗?或者其中之一就足够了?
采纳答案by djna
@Basic
signifies that an attribute is to be persisted and a standard mapping is to be used. It has parameters which allow you to specify whether the attribute is to be lazily loaded and whether it's nullable.@Column
allows you to specify the name of the column in the database to which the attribute is to be persisted.
@Basic
表示将保留属性并使用标准映射。它具有允许您指定是否延迟加载属性以及它是否可以为空的参数。@Column
允许您指定要持久化属性的数据库中列的名称。
If you specify one without the other then you get default behaviour which is sensible, so commonly folks use only one with the exception of special cases.
如果你指定一个而没有另一个,那么你会得到默认的行为,这是明智的,所以除了特殊情况外,人们通常只使用一种。
So if we wanted a lazy loading of an attribute and to specify a column name we can say
因此,如果我们想要延迟加载属性并指定列名,我们可以说
@Basic(fetch=FetchType.LAZY)
@Column(name="WIBBLE")
If we neeed the default, non-lazy behaviour then just the @Column
would have been sufficient.
如果我们需要默认的、非惰性的行为,那么就@Column
足够了。
回答by Hosam Aly
In addition to @djna's answer, it is worth noting that @Basic
should be compared with @OneToMany
, @ManyToOne
and @ManyToMany
. Only one of these can be specified on any property.
除了@djna 的回答之外,值得注意的是@Basic
应该与@OneToMany
,@ManyToOne
和进行比较@ManyToMany
。在任何属性上只能指定其中之一。
@Column
and @JoinColumn
can be specified along with any of these to describe the database column properties.
@Column
并且@JoinColumn
可以与其中任何一个一起指定以描述数据库列属性。
These are two sets of annotations that can be used together, but only one annotation of each set can be used at a time.
这是可以一起使用的两组注解,但每次只能使用每组注解中的一个。
回答by Gab
It is worth noting that Basic is designed for primitive fields
值得注意的是,Basic 是为原始字段设计的
http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes
http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes
A basic attribute is one where the attribute class is a simple type such as String, Number, Date or a primitive. A basic attribute's value can map directly to the column value in the database.
The types and conversions supported depend on the JPA implementation and database platform. Any basic attribute using a type that does not map directly to a database type can be serialized to a binary database type.
The easiest way to map a basic attribute in JPA is to do nothing. Any attributes that have no other annotations and do not reference other entities will be automatically mapped as basic, and even serialized if not a basic type. The column name for the attribute will be defaulted, named the same as the attribute name, as uppercase.
基本属性是指属性类是简单类型(例如 String、Number、Date 或原始类型)的属性。基本属性的值可以直接映射到数据库中的列值。
支持的类型和转换取决于 JPA 实现和数据库平台。使用不直接映射到数据库类型的类型的任何基本属性都可以序列化为二进制数据库类型。
在 JPA 中映射基本属性的最简单方法是什么都不做。任何没有其他注释且不引用其他实体的属性将自动映射为基本类型,如果不是基本类型,甚至会被序列化。属性的列名将被默认,命名为与属性名相同的大写。
回答by Sonu patel
The @Basic annotation are applied to JPA entities, and the of @Column are applied to the database columns @Basic annotation's optional attribute defines whether the entity field can be null or not; on the other hand,
@Basic注解应用于JPA实体,@Column的注解应用于数据库列@Basic注解的可选属性定义实体字段是否可以为空;另一方面,
- @Column annotation's nullable attribute specifies whether the corresponding database column can be null
- We can use @Basic to indicate that a field should be lazily loaded
- The @Column annotation allows us to specify the name of the mapped database column
- @Basic annotation marks the property as not optional on the Java object level. And (nullable = false) on the column mapping, is only responsible for the generation of a NOT NULL database constraint.
- @Column 注解的可空属性指定对应的数据库列是否可以为空
- 我们可以使用@Basic 来指示一个字段应该被延迟加载
- @Column 注释允许我们指定映射的数据库列的名称
- @Basic 注释在 Java 对象级别将属性标记为非可选。而(nullable = false) 上的列映射,只负责生成一个NOT NULL 数据库约束。