Hi guys,
I have some trouble mapping 2 components using the same class but referencing different column in the table.
Let me explain :
I have a table 'customer' containing the information of...customers.
This table includes the home address and the postal address information.
The home address is in the fields : ADDRESS, SUBURB, POSTCODE, STATE
The postal address is in the fields : P_ADDRESS , P_SUBURB, P_POSTCODE, P_STATE
What I want is a Customer class with 2 Address references.My Address class is mapped like this :
Code:
@Embeddable
public class Address {
private String address;
private String postCode;
private String state;
private String suburb;
@Column(name="ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="POSTCODE")
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
@Column(name="STATE")
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Column(name="SUBURB")
public String getSuburb() {
return suburb;
}
public void setSuburb(String suburb) {
this.suburb = suburb;
}
}
My Customer class is mapped like this :
Code:
@Entity
@Table(name="CUSTOMER")
public class Customer {
private int id;
private String surname;
private String givenNames;
@Embedded
private Address homeAddress;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="address",column=@Column(name="P_ADDRESS")),
@AttributeOverride(name="suburb",column=@Column(name="P_ADDRESS")),
@AttributeOverride(name="postCode",column=@Column(name="P_POSTCODE")),
@AttributeOverride(name="state",column=@Column(name="P_STATE"))
})
private Address postalAddress;
@Id
@Column(name="ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="SURNAME")
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Column(name="GIVEN_NAMES")
public String getGivenNames() {
return givenNames;
}
public void setGivenNames(String givenNames) {
this.givenNames = givenNames;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Address getPostalAddress() {
return postalAddress;
}
public void setPostalAddress(Address postalAddress) {
this.postalAddress = postalAddress;
}
When I comment the lines concerning the postal address everything work fine and I can retrieve the home address information.
When I uncomment those lines I get the following error message:
Code:
org.hibernate.MappingException: Repeated column in mapping for entity: test.Customer column: ADDRESS (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:681)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:703)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:699)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:725)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:478)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1290)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1732)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:88)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
I will need to update the address information so the insert="false" and udpate="false" are not an option.
Is there another one?
Thank you.
Max.