Person
class
@Embeddable public class Person { @Column public int code; //... }
is embedded inside Event
twice as two different attributes: manager
and operator
@Entity public class Event { @Embedded @Column(name = "manager_code") public Person manager; @Embedded @Column(name = "operator_code") public Person operator; //... }
This should give two respective columns when generating database schema with Persistence. Instead an exception is thrown:
org.hibernate.MappingException: Repeated column in mapping for entity: Event column: code
How to override default column name code
for each attribute?
Overriding an attribute mapping You can use the @AttributeOverride annotation on the Book entity to override the mapping of each attribute defined by the Publication class. You only need to provide the name of the attribute for which you want to change the mapping and a @Column annotation.
Used to override the mapping of a Basic (whether explicit or default) property or field or Id property or field.
Annotation Type EmbeddedIdApplied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable .
Annotation Type AttributeOverride May be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes).
Use @AttributeOverride
, here is an example
@Embeddable public class Address { protected String street; protected String city; protected String state; @Embedded protected Zipcode zipcode; } @Embeddable public class Zipcode { protected String zip; protected String plusFour; } @Entity public class Customer { @Id protected Integer id; protected String name; @AttributeOverrides({ @AttributeOverride(name="state", column=@Column(name="ADDR_STATE")), @AttributeOverride(name="zipcode.zip", column=@Column(name="ADDR_ZIP")) }) @Embedded protected Address address; ... }
In your case it would look like this
@Entity public class Event { @Embedded @AttributeOverride(name="code", column=@Column(name="manager_code")) public Person manager; @Embedded @AttributeOverride(name="code", column=@Column(name="operator_code")) public Person operator; //... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With