java Hibernate:@Embedded 注解技术和@OneToOne 注解技术的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13723914/
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-10-31 13:47:14  来源:igfitidea点击:

Hibernate : Difference between @ Embedded annotation technique and @OneToOne annotation Technique

javahibernatejpaannotations

提问by Harmeet Singh Taara

What is the difference between @Embedded annotation technique and @OneToOne annotation technique because in Embedded the java class contain "Has a" relationship in class and with the help of @Embedded annotation we persist the has a object in database. and in OneToOne relationship we also persist the has a object in database.

@Embedded 批注技术和@OneToOne 批注技术有什么区别,因为在 Embedded 中,java 类在类中包含“具有”关系,并且在 @Embedded 批注的帮助下,我们将 has 对象持久化到数据库中。并且在 OneToOne 关系中,我们还在数据库中持久化了 has 一个对象。

采纳答案by Ben Thurley

@OneToOne is for mapping two DB tables that are related with a one to one relationship. For example a Customer might always have one record in a Name table.

@OneToOne 用于映射与一对一关系相关的两个数据库表。例如,客户在名称表中可能总是有一条记录。

Alternatively if those name fields are on the Customer table (not in a separate table) then you might want an @embedded. On the face of it you could just add the name fields as standard attributes to the Customer entity but it can be useful if those same columns appear on multiple tables (for example you might have the name columns on a Supplier table).

或者,如果这些名称字段在 Customer 表中(不在单独的表中),那么您可能需要一个 @embedded。从表面上看,您可以将名称字段作为标准属性添加到 Customer 实体,但如果这些相同的列出现在多个表中(例如,您可能在供应商表上有名称列),这会很有用。

回答by Perception

Its the difference between composition and aggregation. @Embedded objects are always managed within the lifecycle of their parents. If the parent is updated or deleted, they are updated or deleted as well. @OneToOne objects maymimic composition via the cascadeType option of their @Join annotation, but by default they are aggregated, aka their lifecycle is separate from that of their parent objects.

它是组合和聚合之间的区别。@Embedded 对象始终在其父对象的生命周期内进行管理。如果父项被更新或删除,它们也会被更新或删除。@OneToOne 对象可以通过它们的 @Join 注释的 cascadeType 选项模拟组合,但默认情况下它们是聚合的,也就是说它们的生命周期与其父对象的生命周期是分开的。

回答by Inder

@Embedded is used with Value Objects (Objects which have a meaning only when attached to an Object) whereas one to one mapping is between two objects having their own existence and meaning.

@Embedded 与值对象(对象只有在附加到对象时才有意义)一起使用,而一对一映射是在两个具有自己存在和意义的对象之间。

For e.g.

例如

Value Object and @Embedded: If we have a User class and this class has an address Object in it, it can be considered as a value object as the address alone does not have any significance until unless associated with a user. Here address object can be annotated with @Embedded.

值对象和@Embedded:如果我们有一个 User 类并且这个类中有一个地址对象,它可以被视为一个值对象,因为除非与用户相关联,否则地址本身没有任何意义。这里地址对象可以用@Embedded 进行注释。

One to One mapping and @OneToOne: If we have a User class and this class has a 'Father' Object or a 'Mother' object, we would want to annotate the 'Father' or 'Mother' instance as @OneToOne as 'Father' or 'Mother' have their own meaning and existence and are not Value objects to User class.

一对一映射和@OneToOne:如果我们有一个 User 类并且这个类有一个 'Father' 对象或一个 'Mother' 对象,我们希望将 'Father' 或 'Mother' 实例注释为 @OneToOne 为 'Father ' 或 'Mother' 有它们自己的意义和存在,并且不是 User 类的 Value 对象。

A closely related difference is between @OneToMany and @ElementCollection. Both are used to save instance variables of Collection type in Java class. The difference being, @ElementCollection is to be used when the elements of Collection being saved are Value Objects whereas @OneToMany is used when the elments and object have well defined meaning and existence.

一个密切相关的区别是@OneToMany 和@ElementCollection。两者都用于在Java类中保存Collection类型的实例变量。不同之处在于,@ElementCollection 在保存的 Collection 元素是值对象时使用,而@OneToMany 在元素和对象具有明确定义的含义和存在时使用。

回答by Shailesh Pratapwar

Use @OneToOne, only if fields can be reused. Otherwise, go for @Embeddable.

仅当字段可以重用时才使用@OneToOne。否则,请选择@Embeddable。

A quote from Beginning Hibernte, 3rd Edition:

引自 Beginning Hibernte,第 3 版:

There is nothing intrinsically wrong with mapping a one-to-one association between two entities where one is not a component of (i.e., embedded into) the other. The relationship is often somewhat suspect, however. You should give some thought to using the embedded technique described previously before using the @OneToOne annotation.

映射两个实体之间的一对一关联并没有本质上的错误,其中一个实体不是另一个实体的组件(即嵌入到另一个实体中)。然而,这种关系往往有些可疑。在使用 @OneToOne 注释之前,您应该考虑使用前面描述的嵌入技术。

@Embeddable:If the fields in an entity (X) are contained within the same table as another entity (Y), then entity X is called "component" in hibernate terms or "embedded" in JPA terms. In any case, JPA or hibernate do not allow to use 2nd table to store such embedded entities.

@Embeddable:如果实体 (X) 中的字段与另一个实体 (Y) 包含在同一个表中,则实体 X 在休眠术语中称为“组件”或在 JPA 术语中称为“嵌入”。在任何情况下,JPA 或 hibernate 都不允许使用第二个表来存储此类嵌入实体。

Generally, we think of normalizing a table when data is being reused by more than one table. Example: A Customer (id, name, street, city, pin, landmark) can be normalized into Customer(id, name) and CustomerAddress(cust_id, street, city, pin, landmark). In this case, we can reuse CustomerAddress by linking the same using cust_id with other tables. But if this reuse is not required in your application, then we can just keep all columns in one table.

通常,当数据被多个表重用时,我们会考虑对表进行规范化。示例:客户(id、名称、街道、城市、pin、地标)可以规范化为客户(id,名称)和客户地址(cust_id,街道,城市,pin,地标)。在这种情况下,我们可以通过使用 cust_id 与其他表链接相同的 CustomerAddress 来重用 CustomerAddress。但是如果您的应用程序不需要这种重用,那么我们可以将所有列保留在一个表中

So, a thumb rule is,

所以,一个经验法则是,

  • If reuse -> @OneToOne,
  • If no reuse -> @Embeddable
  • 如果重用 -> @OneToOne,
  • 如果没有重用 -> @Embeddable

回答by Dimitri Dewaele

@Embedded is typically to represent a composite primary key as an embeddable class:

@Embedded 通常将复合主键表示为可嵌入的类:

@Entity
public class Project {
    @EmbeddedId ProjectId id;
     :
}

@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
}

The primary key fields are defined in an embeddable class. The entity contains a single primary key field that is annotated with @EmbeddedId and contains an instance of that embeddable class. When using this form a separate ID class is not defined because the embeddable class itself can represent complete primary key values.

主键字段在可嵌入类中定义。该实体包含一个用@EmbeddedId 注释的主键字段,并包含该可嵌入类的实例。使用这种形式时,没有定义单独的 ID 类,因为可嵌入类本身可以表示完整的主键值。

@OneToOne is for mapping two DB tables that are related with a one to one relationship. @Id will be the primary key.

@OneToOne 用于映射与一对一关系相关的两个数据库表。@Id 将是主键。