All your fields which are not mapped to the database should be annotated with @Transient
. In this case Hibernate will not try to map the field type and will not complain for a duplicate name.
Overriding Embedded XML attribute in Entity that has no Database field mapping
-
03-06-2022 - |
Question
I have the following embeddable classes.
Email:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Email")
@Embeddable
public class Email {
@XmlElement(required = true, nillable = true, name = "etype")
private String type;
private String address;
private String source;
// getters and setters
}
Address:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyAddress")
@Embeddable
public class MyAddress {
@XmlElement(required = true, nillable = true, name = "atype")
private String type;
private String zip;
// getters and setters
}
Entity that embeds both of the above.
@Entity
@Table(name = "PERSON")
public class MyPerson {
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "address", column = @Column(name = "E_ADDR")),
@AttributeOverride(name = "source", column = @Column(name = "E_SRC")) })
private Email email;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "zip", column = @Column(name = "ZIP")),
private MyAddress address;
}
There is a type
field in both of the Embeds. But that field is not mapped to any Database field. But I need to override it. Because hibernate throws error when running. (Compiles fine).
Is there a way to override type
or give a different name to the embedded and non-database-mapped field?
Note: I am looking for solution with the field name intact. Because I cannot simply change the name.
This would also answer my another question, embedding the same Embeddable again overriding all attributes. For instance, I want to include Home Address, Business Address, etc with same MyAddress
embed.
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow