@AttributeOverride 是什么意思?
我目前正在(重新)熟悉 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
类,它定义了一对@Columns
:lastName
和 ssn
。
哦,我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也就是说,构成嵌入 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".在 UserDetails 类中,我覆盖了
homeAddress
&officeAddress
和Address
这一个地址 POJO 将充当数据库中的两个表。
数据库:
In the UserDetails class, I have overridden
homeAddress
&officeAddress
withAddress
This One Address POJO will act for two table in DB.
DB:
不完全是 -
EmbeddedEmployeePK
定义了一对属性,然后将它们映射到列。@AttributeOverride
注释允许您覆盖嵌入类的属性映射到的列。其用例是当可嵌入类在其列名称不同的不同情况下使用时,并且需要某种机制来让您更改这些列映射。例如,假设您有一个实体,其中包含同一可嵌入对象的两个单独实例 - 它们不能都映射到相同的列名称。
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.
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.
您还可以覆盖其他列属性(不仅仅是名称)。
假设您希望根据嵌入组件的人员来更改 SSN 的长度。您可以为该列定义一个
@AttributeOverride
,如下所示:请参阅 “2.2.2.4. 嵌入式对象(也称为组件)”。
为了保留其他
@Column
属性(例如name
和nullable
),请将它们保留在覆盖的列上,与您在原始列上指定的相同柱子。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:See "2.2.2.4. Embedded objects (aka components)" in the Hibernate Annotations documentation.
In order to preserve other
@Column
properties (such asname
andnullable
) keep them on the overridden column the same as you specified on the original column.