-->
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: Many to many-R with an intermediate which has foreign key
PostPosted: Sat Mar 06, 2010 12:14 pm 
Newbie

Joined: Sat Mar 06, 2010 12:06 pm
Posts: 1
Dear,

I have a problem with hibernate for making mapping objet many to many with an intermediate table which only have foreign key.
I have in SQL 3 tables:
element( idElement: primary, date: primary, ...)
recherche( idRecherche: primary, ... )
conceptrecherche( idElement: primary and foreign key to element, idrecherche: primary and foreign key to recherche)

I want for one element of Recherche to get all elements (element of the class Element) (thank to conceptrecherche intermediate table with foreign key)

In this way I do something like:
Code:
@Entity
@Table(name = "recherche")
public class Recherche implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Basic(optional = false)
   @Column(name = "idRecherche")

   @JoinTable(name = "conceptrecherche", joinColumns = {@JoinColumn(name = "idRecherche", referencedColumnName = "idRecherche")}, inverseJoinColumns = {@JoinColumn(name = "idElement", referencedColumnName = "idElement")})
   @ManyToMany
   private List<Element> elementCollection;
...
}

@Entity
@Table(name = "element")
public class Element implements Serializable {
   @EmbeddedId
   protected ElementPK elementPK;

   @JoinColumn(name = "idElement", referencedColumnName = "idElement")
   @ManyToMany
   private List<Recherche> rechercheCollection;
..
}

and the primary key for element:
@Embeddable
public class ElementPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "idElement")
    private int idElement;
    @Basic(optional = false)
    @Column(name = "histDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date histDate;
...
}


But when I tried it, I have an error:

Code:
org.hibernate.AnnotationException: referencedColumnNames(idElement) of IK.Db.Entities.Recherche.elementCollection referencing IK.Db.Entities.Element not mapped to a single property


Thank a lot for your help!
Fabien


Top
 Profile  
 
 Post subject: Re: Many to many-R with an intermediate which has foreign key
PostPosted: Mon Mar 08, 2010 3:53 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
1. From the context I suppose that you're intended to map a bidirectional ManyToMany relation
(and not 2 indipendent unidirectional ToMany relations).
If so, then according documentation you must declare the @ManyToMany mappedBy attribute on one side.

2. On a first try I always would avoid use of the optional @JoinTable and @JoinColumn tags.
A correct mapping must also work without these tags.


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.