-->
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: Many-to-many id problem
PostPosted: Fri Oct 15, 2004 12:12 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
Hello,

I have a many-to-many relationship between the A and B classes, where the table in the middle is composed of its id, the id of one side (A class) and the id of the other (B class):

Table A_B:

ID integer not null (pk);
A_ID integer not null;
B_ID integer not null;

When I try to insert a B object into A's set of Bs,

The generated SQL (show_sql=true):
insert into A_B (A_ID, B_ID) values (?, ?)

I get a database error:

Code:
ORA-01400: cannot insert NULL into ("USER"."A_B"."ID")


because it is trying to insert the record without generating an ID..

Am I supposed to have just A_ID and B_ID in the A_B table, or can I let the ID column and have hibernate generate it when inserting a new record? Please help :S, thanks in advance!

Hibernate version:2.1.6

Mapping documents:

A.hbm.xml (xdoclet generated)

Code:
        <set
            name="Bs"
            table="A_B"
            lazy="false"
            inverse="false"
            cascade="save-update"
            sort="unsorted"
        >

              <key
                  column="A_ID"
              >
              </key>

              <many-to-many
                  class="B"
                  column="B_ID"
                  outer-join="auto"
               />

        </set>


Name and version of the database you are using:
Oracle 9i

(no "ID" anywhere)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 9:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You have not shown it but I assume you don't have an appropriate ID generator defined. Try a native generator first.

See:
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id


Top
 Profile  
 
 Post subject: Id generation for many-to-many sets?
PostPosted: Sat Oct 16, 2004 1:25 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
Thanks so much for your reply David, I'm using "increment" for id generation in both A and B classes.. I didnt map the table in the middle (A_B) because it is not necessary, since hibernate controls the insertion of records in it. That's why I dont have an id generator for that table.. is there a way I can tell hibernate (in the "set" declaration) to generate an id and insert it in the A_B.ID? Or should I just remove the ID field from the table?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 1:27 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can use an <idbag> mapping with a suorrogate <collection-id> generator for the link table. Keep in mind that this model is not great: You not only allow many-to-many multiplicity, but one particular B could be associated with one particular A _several times_.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In other words: better make this A_B table a full entity mapping to a new class and use two one-to-many instead of a many-to-many.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 16, 2004 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Or add a unique key on the A_ID, B_ID composite columns.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Sat Oct 16, 2004 2:25 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
Thanks guys, finally I've decided to drop the ID column and it works nicely, since I wasnt really using it.. nice to know the other alternatives, for when I have to store aditional information..


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.