Java @AttributeOverride 是什么意思?

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

What does @AttributeOverride mean?

javajpaattributesoverriding

提问by jaydel

I'm currently coming (back) up to speed with EJB and while I was away it changed drastically (so far for the better). However, I've come across a concept that I am struggling with and would like to understand better as it seems to be used in our (where I work, not me and all the voices in my head) code quite a bit.

我目前正在(回来)跟上 EJB 的步伐,而在我离开的时候,它发生了巨大的变化(到目前为止变得更好了)。然而,我遇到了一个我正在努力解决的概念,并希望更好地理解它,因为它似乎在我们的(我工作的地方,而不是我和我头脑中的所有声音)代码中使用了很多。

Here's the example I've found in a book. It's part of an example showing how to use the @EmbeddedIdannotation:

这是我在一本书中找到的例子。这是展示如何使用@EmbeddedId注释的示例的一部分:

@Entity
public class Employee implements java.io.Serializable
{
    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name="lastName", column=@Column(name="LAST_NAME"),
        @AttributeOverride(name="ssn", column=@Column(name="SSN"))
    })

    private EmbeddedEmployeePK pk;

    ...
}

The EmbeddedEmployeePKclass is a fairly straightforward @Embeddableclass that defines a pair of @Columns: lastNameand ssn.

EmbeddedEmployeePK类是一个相当简单的@Embeddable类来定义对@ColumnslastNamessn

Oh, and I lifted this example from O'Reilly's Enterprise JavaBeans 3.1 by Rubinger & Burke.

哦,我从 Rubinger & Burke 的 O'Reilly 的 Enterprise JavaBeans 3.1 中提取了这个例子。

Thanks in advance for any help you can give me.

在此先感谢您能给我的任何帮助。

采纳答案by zmf

It's saying that the attributes that make up the embedded id may have predefined (through explicit or implicit mappings) column names. By using the @AttributeOverrideyou're saying "ignore what other information you have with regard to what column it is stored in, and use the one I specify here".

它是说组成嵌入 id 的属性可能已经预定义(通过显式或隐式映射)列名。通过使用 ,@AttributeOverride您是在说“忽略关于它存储在哪个列中的其他信息,并使用我在此处指定的信息”。

回答by skaffman

The EmbeddedEmployeePK class is a fairly straightforward @Embeddable class that defines a pair of @Columns: lastName and ssn.

EmbeddedEmployeePK 类是一个相当简单的@Embeddable 类,它定义了一对@Columns:lastName 和ssn。

Not quite - EmbeddedEmployeePKdefines a pair of properties, which are then mapped to columns. The @AttributeOverrideannotations allow you to override the columns to which the embedded class's properties are mapped.

不完全 -EmbeddedEmployeePK定义一对属性,然后将它们映射到列。该@AttributeOverride注解允许你将覆盖到嵌入式类的属性映射的列。

The use case for this is when the embeddable class is used in different situations where its column names differ, and some mechanism is required to let you change those column mappings. For example, say you have an entity which contains two separate instances of the same embeddable - they can't both map to the same column names.

用例是当可嵌入类在其列名称不同的不同情况下使用时,需要某种机制来让您更改这些列映射。例如,假设您有一个实体,其中包含同一可嵌入项的两个单独实例 - 它们不能都映射到相同的列名。

回答by Mike Yockey

JPA tries to map field names to column names in a datasource, so what you're seeing here is the translation between the name of a field variable to the name of a column in a database.

JPA 尝试将字段名称映射到数据源中的列名称,因此您在此处看到的是字段变量名称与数据库中列名称之间的转换。

回答by Ravi Parekh

AttributeOverride

属性覆盖

In the UserDetails class, I have overridden homeAddress& officeAddresswith Address

在的UserDetails类,我已重写homeAddressofficeAddressAddress

This One Address POJO will act for two table in DB.

这个 One Address POJO 将作用于 DB 中的两个表。

DB:

D B:

Table1      Table2
STREET_NAME HOME_STREET_NAME
CITY_NAME   HOME_CITY_NAME
STATE_NAME  HOME_STATE_NAME
PIN_CODE    HOME_PIN_CODE

回答by naXa

You can override also other column properties (not just names).
Let's assume that you want to change the length of SSN based on who is embedding your component. You can define an @AttributeOverridefor the column like this:

您还可以覆盖其他列属性(不仅仅是名称)。
假设您想根据嵌入组件的人来更改 SSN 的长度。您可以@AttributeOverride像这样为列定义一个:

@AttributeOverrides({
    @AttributeOverride(name = "ssn", column = @Column(name = "SSN", length = 11))
})
private EmbeddedEmployeePK pk;

See "2.2.2.4. Embedded objects (aka components)"in the Hibernate Annotations documentation.

请参阅Hibernate Annotations 文档中的“2.2.2.4. Embedded objects (aka components)”

In order to preserve other @Columnproperties (such as nameand nullable) keep them on the overridden column the same as you specified on the original column.

为了保留其他@Column属性(例如namenullable),将它们保留在覆盖列上与您在原始列上指定的相同。