-->
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.  [ 9 posts ] 
Author Message
 Post subject: one-to-many foreign key column contain 0 value on insertion
PostPosted: Thu Feb 25, 2010 7:29 am 
Newbie

Joined: Tue Oct 16, 2007 10:56 pm
Posts: 9
I have 2 tables with one-to-many relationship, ex: student and school.

under the same single open transaction, i create a new instance for each object representing the 2 tables. I add the student instance
to school.getStudents collection.

after i commit the transaction which save the "school" instance, one record is inserted in each of the 2 tables. but the problem is with the student table, value of its school_id column, which is the foreign key to school table, is 0.

can you tell why it works this way?


Thanks,


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Thu Feb 25, 2010 9:16 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Do you defined the one-to-many relationship unidirectional or bidirectional (= with inverse owner).
In ladder case, you must also set the relationship on both sides:
not only

Code:
school.getStudents().add(student)


also

Code:
student.setShool(school)


must be called.


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Thu Feb 25, 2010 11:51 pm 
Newbie

Joined: Tue Oct 16, 2007 10:56 pm
Posts: 9
Yes, i wrap these 2 statements inside the open transaction.

Code:
school.getStudents().add(student)
student.setShool(school)


I define bidirectional relationship.
Code:
//School mapping xml file.
<list name="students" cascade="all">
             <key column="student_id"></key>
           <index column="school_id"></index>
           <one-to-many class="com.mapring.model.Student" />
</list>

//Student mapping xml file.
<many-to-one name="school">
           <column name="school_id"></column>
</many-to-one>


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 3:01 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You should use the inverse attribute if you want to have the association bidirectional:
http://docs.jboss.org/hibernate/stable/ ... irectional


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 3:41 am 
Newbie

Joined: Tue Oct 16, 2007 10:56 pm
Posts: 9
Ok, the problem is solved now because i put the reverse column name for
Code:
<key column="school_id"></key>
<index column="student_id"></index>


But there happens another problem, now the primary key of the foreign table, "Student", has a value of "0" instead of auto incremented value. I've specified the below in the student mapping file, but why it still doesn't work?
Code:
<id name="studentId" type="int">
            <column name="student_id" />
            <generator class="increment" />
        </id>


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 5:02 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is not a good idea to use the id column as the list index. List indexes usually go from 0 to the number of entries in the list (minus one). I guess the <index column="student_id"></index> mapping overrides the <generator class="increment" /> mapping. You should probably use a <set> mapping instead of <list>. <bag> is also possible but it allows you to enter the same student more than once for a school.


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 5:59 am 
Newbie

Joined: Tue Oct 16, 2007 10:56 pm
Posts: 9
You're so right. yes after changing from List to Set, now it works. Thanks.

According to Hibernate manual, Bag is not good in term of performance. because it doesn't maintain an index column so when a user modify an item in the collection, the whole collection has to be deleted and recreated again.

Is the above statement true? in my example, what collection type best suited in my situation?

Thanks,


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 6:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
According to Hibernate manual, Bag is not good in term of performance. because it doesn't maintain an index column so when a user modify an item in the collection, the whole collection has to be deleted and recreated again.


But the <many-to-one> in the Student is the owner of the association (or didn't you add the inverse="true" as pb00067 suggested?) so the the <set> will not be used for updates.

I would use a <set> myself because it feels more comfortable and I don't really worry about the performance of code like in the example here: http://docs.jboss.org/hibernate/stable/ ... ientupdate


Top
 Profile  
 
 Post subject: Re: one-to-many foreign key column contain 0 value on insertion
PostPosted: Fri Feb 26, 2010 6:45 am 
Newbie

Joined: Tue Oct 16, 2007 10:56 pm
Posts: 9
Thanks so much. I understand better now.


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