Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Multiple Embedded fields with prefix?

With JPA annoations, I want to reuse same embedded object like this :

@Entity public class User {     @Embedded     public Address homeAddress;      @Embedded     public Address workAddress; }  @Embeddable public class Address {     public String code;     public String city;     ... }  

I can specify SQL column names with @AttributeOverrides, @AttributeOverride and @Column, but it's verbos. Is it possible to specify just a prefix to add to each column for homeAddress and workAddress ?

Thanks,

Xavier

like image 626
xnopre Avatar asked Oct 16 '12 09:10

xnopre


1 Answers

If you would like to use multiple same Embedded class. You have to do @AttributeOverrides for all columns. Try as below;

Reference JPA AttributeOverrides

@Embeddable public class Address {     private String state;     @Column(name = "zip_code")     private String zip; }  @Entity(name = "Employee") public class Employee implements Serializable {     @Embedded     @AttributeOverrides({         @AttributeOverride(name = "state", column = @Column(name = "province_1")),                                @AttributeOverride(name = "zip", column = @Column(name = "postal_code_2"))     })     private Address address_1;        @Embedded     @AttributeOverrides({         @AttributeOverride(name = "state", column = @Column(name = "province_2")),                                @AttributeOverride(name = "zip", column = @Column(name = "postal_code_2"))     })     private Address address_2;    }    

My suggestion, if there are one or more Embedded value in your Entity. Try to use @CollectionTable.

@CollectionTable(name = "EMPLOYEE_ADDRESS", joinColumns = @JoinColumn(name = "ADDRESS_ID")) private List<Address> addressList; 

Reference JPA CollectionTable

like image 60
Zaw Than oo Avatar answered Sep 24 '22 13:09

Zaw Than oo