@AttributeOverride 是什么意思?

发布于 2024-10-07 06:46:37 字数 768 浏览 2 评论 0原文

我目前正在(重新)熟悉 EJB,在我离开期间,它发生了巨大的变化(到目前为止变得更好)。然而,我遇到了一个我正在努力解决的概念,并且希望更好地理解它,因为它似乎在我们(我工作的地方,而不是我和我脑海中的所有声音)代码中使用了很多。

这是我在书中找到的例子。它是展示如何使用 @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;

    ...
}

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

哦,我从 Rubinger & 的 O'Reilly 的 Enterprise JavaBeans 3.1 中提取了这个示例。伯克.

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

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.

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

@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 EmbeddedEmployeePK class is a fairly straightforward @Embeddable class that defines a pair of @Columns: lastName and ssn.

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

Thanks in advance for any help you can give me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深空失忆 2024-10-14 06:46:37

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

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 @AttributeOverride you're saying "ignore what other information you have with regard to what column it is stored in, and use the one I specify here".

魂ガ小子 2024-10-14 06:46:37

AttributeOverride

在 UserDetails 类中,我覆盖了 homeAddress & officeAddressAddress

这一个地址 POJO 将充当数据库中的两个表。

数据库:

Table1      Table2
STREET_NAME HOME_STREET_NAME
CITY_NAME   HOME_CITY_NAME
STATE_NAME  HOME_STATE_NAME
PIN_CODE    HOME_PIN_CODE

AttributeOverride

In the UserDetails class, I have overridden homeAddress & officeAddress with Address

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

DB:

Table1      Table2
STREET_NAME HOME_STREET_NAME
CITY_NAME   HOME_CITY_NAME
STATE_NAME  HOME_STATE_NAME
PIN_CODE    HOME_PIN_CODE
演多会厌 2024-10-14 06:46:37

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

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

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

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

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

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.

四叶草在未来唯美盛开 2024-10-14 06:46:37

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

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.

素衣风尘叹 2024-10-14 06:46:37

您还可以覆盖其他列属性(不仅仅是名称)。
假设您希望根据嵌入组件的人员来更改 SSN 的长度。您可以为该列定义一个 @AttributeOverride,如下所示:

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

请参阅 “2.2.2.4. 嵌入式对象(也称为组件)”

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

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 @AttributeOverride for the column like this:

@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.

In order to preserve other @Column properties (such as name and nullable) keep them on the overridden column the same as you specified on the original column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文