Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: how to override column names of @Embedded attributes

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?

like image 348
Oleg Mikhailov Avatar asked Apr 12 '16 11:04

Oleg Mikhailov


People also ask

How to override attributes from Embedded class in hibernate?

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.

What is attribute override?

Used to override the mapping of a Basic (whether explicit or default) property or field or Id property or field.

What is @EmbeddedId in JPA?

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 .

What is attribute override in hibernate?

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


1 Answers

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;      //... } 
like image 51
Predrag Maric Avatar answered Sep 23 '22 22:09

Predrag Maric