Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: FK cannot be saved while saving association
PostPosted: Tue Sep 16, 2003 3:34 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Hi,

I have two classes: A and B. A has one-to-many bidirectional association with B.

I did the following to save both A and B to db:

(1) save B's
(2) setBs
(3) save A

From what I saved into db, I found FKs for B are all null.

I am wondering how I can save FK for B when save both A and B?

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2003 3:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sorry, did you actually expect anybody to be able to help you with this kind of vague posting?!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 8:33 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
My bad if you think my question is not clear. Let me rephrase it.

Department class has a one-to-many bidirectional association with Student. I try to save both to the database. But I could not save foreign
key to student table. I am wondering where I did wrong.


Here is what I did to try to
achieve this:

Department dept = new Department(name);

//a loop to created a set of Students
for(.....){
.......
Student st = new Student(name);
DbService.saveStudent(st);
studentSet.add(st);
}

dept.setStudents(studentSet);
DbService.saveDepartment(dept);

--------------------------------
The following are mapping files:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="Department" table="department">

<id name="id" type="int" unsaved-value="null">
<column name="department_id" not-null="true"/>
<generator class="native"/>
</id>

<property name="name" column="name" type="string" length="50"/>
<set name="students" table="student"
cascade="all" inverse="true" lazy="true">
<key column="department_id"/>
<one-to-many class="Student"/>
</set>
</class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="Student" table="student">

<id name="id" type="int" unsaved-value="null">
<column name="student_id" not-null="true"/>
<generator class="native"/>
</id>

<property name="name" column="name" type="string" length="30"/>

<many-to-one name="department" class="Department" column="department_id"/>
</class>

</hibernate-mapping>

--------------
Classes:

public class Department{
int id;
String name;
Set studentSet;

//getters/setters for the above
......
}

public class Student{
int id;
String name;
Department dept;

//getters/setters for the above
......

}


regards,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 8:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I guess this:

DbService.saveDepartment(dept);

and this

DbService.saveStudent(st);

calls Session.saveOrUpdate()?

You are using an "int" primitive for the identifer but set the unsaved value to "null". A primitive is never null, Hibernate can't decide if an instance is unsaved or not. Plese do us a favour and re-read the "parent/child relationship" chapter of the documentation. Half of your code is not neccessary, because you may cascade operations from department to students.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 9:57 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Thankss,

So, you are saying set "int" to "0" . I actually followed the example given in
7.5 (Updating objects saved ......):
<id name="id" type="long" column="uid" unsaved-value="null">
....
</id>

Here the type is long, but int is similar, right?

The reason I did not follow the sample from 8.2 is: it requires to provide an parent id to save a child. If I try to save new parent and children at the
same time, parent id is unknown.

Any more thoughts?

regards,

christian wrote:
I guess this:

DbService.saveDepartment(dept);

and this

DbService.saveStudent(st);

calls Session.saveOrUpdate()?

You are using an "int" primitive for the identifer but set the unsaved value to "null". A primitive is never null, Hibernate can't decide if an instance is unsaved or not. Plese do us a favour and re-read the "parent/child relationship" chapter of the documentation. Half of your code is not neccessary, because you may cascade operations from department to students.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 10:02 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you don't know the difference between an "int" and a "Long", you should first start with a Java Beginners tutorial. Your problem is covered in length in the reference documentation. As far as I remember, I (or someone else) pointed you to this chapter multiple times.

Please follow our advice. Your questions still show a real lack of understanding how Hibernate works. The real INSERT statements will not be executed when you call save(), but when "all information such as foreign keys" is known, at the end of your transaction during flush.

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 10:17 am 
Newbie

Joined: Mon Sep 15, 2003 6:44 pm
Posts: 8
I think there are two ways (may be even more) to solve this problem.

If you set the CASCADE property properly set,, then you don't have to save the students seprartely. It should get saved along with the Department when it gets saved

OR

1. Save the Department and get the newly cretaed Departement Id
2. Either set the newly created id of the departement to the department object OR do a find on the Departemnt object to get the newly created object
3. set that object to the student. meaning, student.setDepartment(dept);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 10:56 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Ha!

Are we talking about Hibernate or Java now? I guess Hibernate types are not
exactly correspond to java types? Isn't this a Hibernate Biginners Forum? I apologize if I offed you, ok? Gosh.


christian wrote:
If you don't know the difference between an "int" and a "Long", you should first start with a Java Beginners tutorial. Your problem is covered in length in the reference documentation. As far as I remember, I (or someone else) pointed you to this chapter multiple times.

Please follow our advice. Your questions still show a real lack of understanding how Hibernate works. The real INSERT statements will not be executed when you call save(), but when "all information such as foreign keys" is known, at the end of your transaction during flush.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2003 11:05 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 8:53 pm
Posts: 34
Thanks,

I will try out yor advice.

regards,


ChennaiVasi wrote:
I think there are two ways (may be even more) to solve this problem.

If you set the CASCADE property properly set,, then you don't have to save the students seprartely. It should get saved along with the Department when it gets saved

OR

1. Save the Department and get the newly cretaed Departement Id
2. Either set the newly created id of the departement to the department object OR do a find on the Departemnt object to get the newly created object
3. set that object to the student. meaning, student.setDepartment(dept);


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.