Java - JPA @Basic 和 @Embedded 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2578530/
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 - JPA @Basic and @Embedded annotations
提问by Yatendra Goel
I am learning JPA from this tutorial.
我正在从本教程中学习 JPA 。
I have some confusions in understanding the following annotations:
我在理解以下注释时有些困惑:
@Basic
@Embedded
@Basic
@Embedded
Fields of an embeddable type default to persistent, as if annotated with @Embedded.
可嵌入类型的字段默认为持久的,就像用 @Embedded 注释一样。
If the fields of embeddable types default to persistent, then why would we need the @Embedded
annotation
如果可嵌入类型的字段默认为persistent,那为什么还需要@Embedded
注解
采纳答案by Pascal Thivent
The @Embeddable
annotation allows to specify a class whose instances are stored as intrinsic part of the owning entity. This annotation has no attributes.
该@Embeddable
注解允许指定一个类,其实例存储为拥有实体的固有部分。此注释没有属性。
@Embeddable
public class EmploymentPeriod {
java.util.Date startDate;
java.util.Date endDate;
...
}
The @Embedded
annotation is used to specify a persistent field or property of an entity whose value is an instance of an embeddable class. By default, column definitions specified in the @Embeddable
class apply to the table of the owning entity but you can override themusing@AttributeOverride
:
该@Embedded
注释用于指定其值是一个可嵌入类的实例实体的持久字段或属性。默认情况下,@Embeddable
类中指定的列定义适用于拥有实体的表,但您可以使用@AttributeOverride
以下方法覆盖它们:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="startDate", column=@Column(name="EMP_START")),
@AttributeOverride(name="endDate", column=@Column(name="EMP_END"))
})
public EmploymentPeriod getEmploymentPeriod() { ... }
Regarding the optional @Basic
annotation, you may use it to configure the fetch type to LAZY
and to configure the mapping to forbid null values (for non primitive types) with the optional
attribute.
关于可选@Basic
注解,您可以使用它来配置获取类型LAZY
并配置映射以禁止具有该optional
属性的空值(对于非原始类型)。
@Basic(fetch=LAZY)
protected String getName() { return name; }
You can also place it on a field or property to explicitly mark it as persistent (for documentation purpose).
您还可以将其放在字段或属性上以将其显式标记为持久性(用于文档目的)。
回答by James P.
In ORM mapping, the granularity of your object model can be finer than that of your database.
在 ORM 映射中,对象模型的粒度可以比数据库的粒度更细。
For example, you can have a Person
record in your database which can be further decomposed to contain a reference to an Address
object in your model. That's where the @Embedded
and @Embeddable
annotations come in. They simply state a relationship where one Entity
can be stored as part of another.
例如,您可以Person
在数据库中有一条记录,该记录可以进一步分解以包含对Address
模型中对象的引用。这就是@Embedded
和@Embeddable
注释的用武之地。它们只是陈述一种关系,其中一个Entity
可以作为另一个的一部分存储。
As for the @Basic
annotation, it's the simplest form of mapping which is applied by default to primitive types such as int and float and their wrappers as well as enums. More information can be had here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property
至于@Basic
注释,它是最简单的映射形式,默认情况下应用于原始类型,例如 int 和 float 及其包装器以及枚举。更多信息可以在这里找到:http: //docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property
回答by Lucky
@Basic
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 java.io.Serializable.
The use of the Basic annotation is optional for persistent fields and properties of these types. If the Basic annotation is not specified for such a field or property, the default values of the Basic annotation will apply.
@基本的
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[]、枚举和任何其他实现 java.io.Serializable 的类型。
对于这些类型的持久字段和属性,基本注释的使用是可选的。如果未为此类字段或属性指定 Basic 注释,则将应用 Basic 注释的默认值。
Example:
例子:
@Basic
protected String name;
and
和
@Basic(fetch=LAZY)
protected String getName() {
return name;
}
@Embedded
Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as Embeddable.
@嵌入式
指定其值为可嵌入类的实例的实体的持久字段或属性。embeddable 类必须注释为 Embeddable。
Example 1:
示例 1:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="startDate", column=@Column("EMP_START")),
@AttributeOverride(name="endDate", column=@Column("EMP_END"))
})
public EmploymentPeriod getEmploymentPeriod() { ... }
Example 2:
示例 2:
@Entity
public class Project {
@EmbeddedId ProjectId id;
//other fields
}
@Embeddable
Class ProjectId {
int departmentId;
long projectId;
}