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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:26:24  来源:igfitidea点击:

Java - JPA @Basic and @Embedded annotations

javajpa

提问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 @Embeddedannotation

如果可嵌入类型的字段默认为persistent,那为什么还需要@Embedded注解

采纳答案by Pascal Thivent

The @Embeddableannotation 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 @Embeddedannotation 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 @Embeddableclass 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 @Basicannotation, you may use it to configure the fetch type to LAZYand to configure the mapping to forbid null values (for non primitive types) with the optionalattribute.

关于可选@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 Personrecord in your database which can be further decomposed to contain a reference to an Addressobject in your model. That's where the @Embeddedand @Embeddableannotations come in. They simply state a relationship where one Entitycan be stored as part of another.

例如,您可以Person在数据库中有一条记录,该记录可以进一步分解以包含对Address模型中对象的引用。这就是@Embedded@Embeddable注释的用武之地。它们只是陈述一种关系,其中一个Entity可以作为另一个的一部分存储。

As for the @Basicannotation, 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;
}

JSR Persistence Specificationand Source reference

JSR 持久化规范源代码参考