-->
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.  [ 6 posts ] 
Author Message
 Post subject: Annotation mapping issue
PostPosted: Wed Feb 16, 2011 7:45 pm 
Newbie

Joined: Thu Jun 03, 2010 5:02 pm
Posts: 5
Hi,
I got a mapping issue and I have no idea how to resolve this.
I created an object SeisSet.java:
Code:
@Entity
@Table(name="seis_set")
public class SeisSet implements Serializable
{
   @Id
   @Column(name = "seis_set_id")
   private String seisSetId;

   @ManyToOne
   @JoinColumn(name="country", referencedColumnName="country")
   private Country country;

   @ManyToOne
   @JoinColumns(
           @JoinColumn(name="country", referencedColumnName="country"),
           @JoinColumn(name="province_state", referencedColumnName="province_state")
    )
   private ProvinceState provinceState;
   .......
}

There are two "ManyToOne" relationship in this entity. Their object:
Code:
@Entity
@Table(name="z_r_country")
@Embeddable
public class Country implements Serializable
{
  @Id
  @Column(name="country")
  private String id;
 
  @Column(name="long_name")
  private String name;
  .....
}

Code:
@Entity
@Table(name="z_r_province_state")
@Embeddable
public class ProvinceState implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  private ProvinceStatePK id;
 
  @Column(name="long_name")
  private String name;

  @ManyToOne
  @JoinColumn(name="country", insertable = false, updatable = false)
  private Country country;
  ........

Code:
@Embeddable
public class ProvinceStatePK implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Column(name = "country")
  private String country;
 
  @Column(name = "province_state")
  private String provinceState;
  .......
}


When I ran the code I got error:
Code:
Repeated column in mapping for entity: SeisSet column: country (should be mapped with insert="false" update="false")

But I cannot add
Code:
insert="false" update="false"

to the property "province_state" because this field will not be inserted/updated with the value when I inserted a province/state into the entity SeisSet.
Does anyone know how to handle this situation?


Top
 Profile  
 
 Post subject: Re: Annotation mapping issue
PostPosted: Thu Feb 17, 2011 5:15 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Of course you have problems, you're using the same column name "country" for all your join columns but also for some of your properties ...
Thus you end up having more than one column named "country" in one table!
Either choose different names for each columns or let hibernate define the join column names for you, it does it very well!!

By the way, you don't need to use referencedColumnName if you're pointing at the PK ...


Top
 Profile  
 
 Post subject: Re: Annotation mapping issue
PostPosted: Thu Feb 17, 2011 1:01 pm 
Newbie

Joined: Wed Feb 16, 2011 7:31 pm
Posts: 1
Thanks for replying.
The column name in database cannot be changed. I tried your second suggestion: let hibernate define the join column.
I removed the @JoinColumn on one property "country" in the object SeisSet.java:
Code:
@ManyToOne
private Country country;

But I have to keep the mapping for the other one as the other one uses composite key:
Code:
@ManyToOne
  @JoinColumns(
      {
        @JoinColumn(name="country"),
        @JoinColumn(name="province_state")
      }
   )
  private ProvinceState provinceState;

I still got the same error. What did I do wrong? Did I misunderstand your suggestion?
The reason I have to keep both the mapping on "country" and "provinceState" is that sometimes we just need insert country name into the table; sometimes we insert both country and state.


Top
 Profile  
 
 Post subject: Re: Annotation mapping issue
PostPosted: Thu Feb 17, 2011 1:58 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

I think you are misunderstanding how the @JoinColumn works ...
The property name represents the name of the column in the join table! It has nothing to do with the name of the field you're using as a FK! By default Hibernate fills this column with the PK of the corresponding class so if that's the behavior you are expecting then you have nothing to specify at all. But if you don't want Hibernate tu use the PK or if you are using a composite key then you have to specify ecplicitly the mappin by using referencedColumnName
Hope I'm making myself clear ... it's not that easy


Top
 Profile  
 
 Post subject: Re: Annotation mapping issue
PostPosted: Thu Feb 17, 2011 6:48 pm 
Newbie

Joined: Thu Jun 03, 2010 5:02 pm
Posts: 5
Thanks for correcting me.
I think my problem is not the same column name in the different tables.
The entity "ProvinceState" has a composite key: "country" and "province_state"; the entity "Country" just has one column "country" as the primary key. The entity "SeisSet" has both "country" column and "province_state" column so it needs to map both "Country" and "ProvinceState". This caused the "Repeated column in mapping" error. Both "country" and "province_sate" have to be insertable/updatable in the entity "SeisSet". Is there a way to implement this?


Top
 Profile  
 
 Post subject: Re: Annotation mapping issue
PostPosted: Fri Feb 18, 2011 7:40 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Have you at least tried to change the names? And I'm not only talking about the names of the columns but also the names of the fields! I will give you 2 reasons why you should do it. First It will make your code much more readable because right now its a mess, you have properties names "country" everywhere and those properties are either String or Country type ... Second it will solve many of your problems! For example in the table z_r_province_state you have a conflict between the "country" column corresponding the country field in your Composite Key and another "country" column corresponding to the FK of the ManyToOne relationship with the class Country. So CHANGE THE NAMES!!!!!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.