java @embedded 注释有什么影响?

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

What impact does the @embedded annotation mean?

javahibernatejpa

提问by user48545

How does the Embedded annotation affect the database?

Embedded annotation 如何影响数据库?

How will SQL queries need to change?

SQL 查询需要如何改变?

What's the typical usecase for using the annotation?

使用注释的典型用例是什么?

回答by Tomasz Nurkiewicz

How does Embedded annotation affect the database?

嵌入式注释如何影响数据库?

It does not affect it at all. On ORM provider layer all fields from embedded entity are mergedwith parent entity and treated the same as if they were declared there all the time. In other words it works as if you would literally copy all the fields, getters and setters into the entity that contains embedded object.

它根本不影响它。在 ORM 提供者层,来自嵌入实体的所有字段都与父实体合并,并像它们一直在那里声明一样对待。换句话说,它的工作原理就像您将所有字段、getter 和 setter 复制到包含嵌入对象的实体中一样。

How will SQL queries need to change?

SQL 查询需要如何改变?

They won't. You don't need to change anything. See above.

他们不会。你不需要改变任何东西。往上看。

What's the typical case for using the annotation annotation?

使用注解注解的典型案例是什么?

Sometimes you have a huge table with several columns (especially with legacy databases). However some columns are logically tied to each other (like street, city and phone number in CUSTOMERtable). When you don't want to create an object with all the fields, you create an embedded Addressobject. This way you logically group address columns into an object instead of having equally huge POJO with a flat list of fields.

有时您有一个包含多个列的大表(尤其是旧数据库)。然而,有些列在逻辑上相互关联(如CUSTOMER表格中的街道、城市和电话号码)。当您不想创建包含所有字段的对象时,您可以创建一个嵌入Address对象。通过这种方式,您可以在逻辑上将地址列分组到一个对象中,而不是拥有同样巨大的 POJO 和一个扁平的字段列表。

Using embedded objects is considered a good practice, especially when strong 1-1 relationship is discovered.

使用嵌入对象被认为是一种很好的做法,尤其是在发现强大的 1-1 关系时。

回答by osdamv

extending the answer of @Tomasz NurkiewiczEmbedded objects are useful to mapping a table's with a composite primary key whit help of the annotation @EmbenddedId

扩展@ Tomasz Nurkiewicz的答案嵌入对象对于使用注释@EmbenddedId 的帮助映射具有复合主键的表很有用

回答by Dimitri Dewaele

What's the typical usecase for using the annotation?

使用注释的典型用例是什么?

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

这通常用于将复合主键表示为可嵌入的类:

@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 类,因为可嵌入类本身可以表示完整的主键值。

How does the Embedded annotation affect the database?

Embedded annotation 如何影响数据库?

It does not. Use this annotation to represent a composite primary key.

它不是。使用此注释来表示复合主键。

How will SQL queries need to change?

SQL 查询需要如何改变?

They won't.

他们不会。

回答by Pawe? Wo?niak

Like Tomasz said - that's the one goal - the other - you can "snaphot" the state of other related entity inside your table. F.e.

就像 Tomasz 所说的那样-这是一个目标-另一个目标-您可以“快照”表中其他相关实体的状态。铁

@Embeddable public class Company {
    String name;
    String streetName;
    String city;
}



@Entity public class Invoice {

    @Embedded 
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="name")),
        @AttributeOverride(name="streetName", column=@Column(name="streetName")),
        @AttributeOverride(name="city", column=@Column(name="city")),
    })            
    Company seller;

    @Embedded 
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="name")),
        @AttributeOverride(name="streetName", column=@Column(name="streetName")),
        @AttributeOverride(name="city", column=@Column(name="city")),
    })        
    Company customer;
}

in this example - without embedded and @AttributeOverrides any change in future of Company customer will change the data in Invoice - which is a bug - the invoice was generated for the company with old data.

在这个例子中 - 没有嵌入和@AttributeOverrides 公司客户未来的任何更改都会更改发票中的数据 - 这是一个错误 - 发票是为公司生成的旧数据。

It's good explained here: :) Java - JPA @Basic and @Embedded annotations

这里有很好的解释::) Java - JPA @Basic 和 @Embedded 注释

回答by Adam

It doesn't always have to be the ID of the class. In Domain Driven Design, you can create a componentout of some of the properties of an object, e.g. in this example http://yaarunmulle.com/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-1.htmla student has an address component.

它并不总是必须是班级的 ID。在领域驱动设计中,你可以创建一个component对象的一些属性,例如在这个例子中http://yaarunmulle.com/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-1.html学生有一个地址组件。

The Address property of Student is annotated with @Embedded to point to the Address class component.

Student 的 Address 属性使用 @Embedded 进行注释以指向 Address 类组件。