-->
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.  [ 7 posts ] 
Author Message
 Post subject: @ManyToMany doesn't generate a table with primary key
PostPosted: Tue Apr 15, 2008 7:27 pm 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Hibernate version:
3.3

Name and version of the database you are using:
Oracle 10g

I have two entities which have a many to many relationship (Foo and Bar).
Using JPA, they are mapped like this: -

Code:
public class Foo {
  ........
@ManyToMany
@JoinTable(name="foos_bars", joinColumns = { @JoinColumn(name = "foo_id") },
   inverseJoinColumns = { @JoinColumn(name = "bar_id") })
   public List<Bar> getBars() {
      return bars;
   }



Code:
public class Bar {
.....
@ManyToMany(mappedBy="foos")   
   public List<Foo> getFoos() {
      return foos;
   }
}




I'm using hibernate.hbm2ddl.auto= create-update

The table generated has only two foreign keys (foo_id and bar_id) but no Primary key of it's own. How do I enforce the generation of primary key?
(My DBA insists strict referential integrity for all tables, and I'm using schema generation completely)

_________________
www.reverttoconsole.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 1:41 pm 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Not a single reply? I'm really concerned about this Issue, atleast would appreciate if anyone has some suggestions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 4:55 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I have also found this in the past if you use Lists on both sides of the relationship. I would recommend switching the owning side of the releationship to use a Set, it the generates a table like so:

Code:
CREATE TABLE course_student
(
  course_id int4 NOT NULL,
  student_id int4 NOT NULL,
  CONSTRAINT course_student_pkey PRIMARY KEY (course_id, student_id),
  CONSTRAINT fkee28be175d95483a FOREIGN KEY (course_id)
      REFERENCES course (course_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fkee28be17d9f37f7a FOREIGN KEY (student_id)
      REFERENCES student (student_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 10:28 am 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Thanks! That worked with the combined primary key. (Still better than not having any primary key at all)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:10 am 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
andydale wrote:

I have also found this in the past if you use Lists on both sides of the relationship. I would recommend switching the owning side of the releationship to use a Set, it the generates a table like so:



I have come across this issue as well when using Lists on both sides. When using a Map on one side, the PKs are generated as expected.

Can anyone throw any light on this? Is this as a result of a bug or is there some fundamental reason why this should be the case?

Thanks.


Alan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:18 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I thought this is because you have no guarantee of unique-ness if both sides are Lists (allow duplicate values), however a Set by default does not allow duplicates (same as PKs).

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:59 am 
Newbie

Joined: Tue Jul 29, 2008 7:01 am
Posts: 9
Yeah, managed to figure that one one after a bit of thought. Makes sense.

Thanks for the reply.


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