-->
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.  [ 4 posts ] 
Author Message
 Post subject: JPA-Hibernate. Repeated column in mapping for entity.
PostPosted: Tue Apr 13, 2010 6:25 am 
Newbie

Joined: Wed Jan 17, 2007 9:17 am
Posts: 3
I might have a design problem, so first of all I would like to explain the design.

Imagine I have a conference with 3 kind of speakers. So I have the entity Conference and the entity Speaker.

The entity Speaker has 3 attributes: speaker1, speaker2 and speaker3 of the entity Speaker. The way I've mapped this is the following.

@Entity
@Table(name = "SPEAKERS")
public class Speaker
{
@Id
@Column(name = "ID_SPEAKER")
private Long idSpeaker;

//other fields like name, etc...

// getter and setter
}


@Entity
@Table(name = "CONFERENCES")
public class Speaker
{
@Id
@Column(name = "ID_CONFERENCE")
private Long idConference;

@ManyToOne
@JoinColumn(name = "ID_SPEAKER")
private Speaker speaker1

@ManyToOne
@JoinColumn(name = "ID_SPEAKER")
private Speaker speaker2

@ManyToOne
@JoinColumn(name = "ID_SPEAKER")
private Speaker speaker3

// getters and setters
}

If I do this, I get the org.hibernate.MappingException: Repeated column in mapping for entity..........

What would be the proper design for such issue? Inheritance?

Thank you very much.


Top
 Profile  
 
 Post subject: Re: JPA-Hibernate. Repeated column in mapping for entity.
PostPosted: Tue Apr 13, 2010 12:05 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello Bertyno, you have two options:

If you only need to have 3 speakers and only 3 you could mappe it as

Code:

@Column(name = "ID_SPEAKER1")
...

@Column(name = "ID_SPEAKER2")
...

@Column(name = "ID_SPEAKER3")
...



or if you gonna have more than 3 speaker you should go to a OneToMany Approach

Code:

@OneToMany(mappedBy = "conference")
private java.util.List<Speaker> speakers = new java.util.ArrayList<Speaker>();



and the Speaker:

Code:

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "conferenceId")
private Conference conference;



Regards,


Top
 Profile  
 
 Post subject: Re: JPA-Hibernate. Repeated column in mapping for entity.
PostPosted: Tue Apr 13, 2010 11:10 pm 
Newbie

Joined: Tue Apr 13, 2010 11:04 pm
Posts: 1
I am a newbie but I believe what you are seeing is a result of Hibernate defining a new 'ID_SPEAKER' column in order to map the @ManyToOne associations. To avoid this you need to change your @JoinColumn spec as follows:

@JoinColumn(name="ID_SPEAKER", insertable=false, updatable=false)

Cheers.


Top
 Profile  
 
 Post subject: Re: JPA-Hibernate. Repeated column in mapping for entity.
PostPosted: Thu Apr 15, 2010 4:35 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
mocando, Hibernate it's not just defining one column with the name of ID_SPEAKER but 3. Even if you use insertable=false and updatable=false you will have one column that you could not write to or update to; if that so, then, when hibernate it's retrieving the data how it will know which property to fill, with only one column for 3 properties. I think that what's bertyno it's trying to achieve it's a little more complex than that.

regards,


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