What does @AttributeOverride mean?

安稳与你 提交于 2019-11-28 09:37:22
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 @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".

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

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!