-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Multiple components mapping for same table
PostPosted: Thu Mar 08, 2012 12:13 am 
Newbie

Joined: Wed Mar 07, 2012 11:24 pm
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Multiple components mapping for same table
PostPosted: Thu Mar 08, 2012 8:31 pm 
Newbie

Joined: Wed Mar 07, 2012 11:24 pm
Posts: 2
Hi,

I also tried to override attributes for both properties :
Code:
@Embedded
@AttributeOverrides({
   @AttributeOverride(name="address",column=@Column(name="ADDRESS")),
   @AttributeOverride(name="suburb",column=@Column(name="SUBURB")),
   @AttributeOverride(name="postCode",column=@Column(name="POSTCODE")),
   @AttributeOverride(name="state",column=@Column(name="STATE"))
})
private PowerProAddress homeAddress;

@Embedded
@AttributeOverrides({
   @AttributeOverride(name="address",column=@Column(name="P_ADDRESS")),
   @AttributeOverride(name="suburb",column=@Column(name="P_SUBURB")),
   @AttributeOverride(name="postCode",column=@Column(name="P_POSTCODE")),
   @AttributeOverride(name="state",column=@Column(name="P_STATE"))
})
private PowerProAddress postalAddress;


But it doesn't work.

Any idea?

Thanks.
Max.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.