-->
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.  [ 4 posts ] 
Author Message
 Post subject: question about inheritance
PostPosted: Thu Mar 16, 2006 7:18 pm 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Hi,

I have two classes : Person and Student.
A Student is a Person, so I map this using a subclass in a "table-per-hierarchy" strategy (this means using a discriminator-value which identifiates the class).
In the class Student there is a constructor which allows a Person object parameter, so that you can create a Student from a Person (in fact the constructor does a copy of all properties between both objects).
Nevertheless, when I'm trying to create and saving a Student from an existing Person like this :

Code:
Student oStudent = new Student(oPerson);
session.SaveOrUpdate(oStudent);



it creates a new Student row in the database, owed to the fact that I'm not using identies in my classes, but letting hibernate generate them.
But that's not what I want ! I would like that it only updates the discriminator column, to change the object type !
Even if I write an identity property for my classes, I got an error saying that it was not able to save the Student object because there was another object with the same ID in the database (typically the Person object)).
Do you see what I mean ?
Any help would be appreciated...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 11:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The Student class is derived from the Person class, right? Hibernate certainly thinks it is, because that's how you've mapped it. So after you've constructed your in-memory Student object, you now have two different objects representing the same row in the table. More importantly, you have an object in Hibernate's session cache (the Person object) that represents the same thing as an object not in Hibernate's cache (the Student object). When you go to save the Student, this conflict is detected, and then much barfing occurs.

Once you've created your Student object, you must get rid of your Person object. Evict it, or just clear the entire session. So long as you've copied the id property from the Person object to the Student object, Hibernate will know that the Student object is to be saved to the same row as the old Person object: the only thing that will change in the DB row is the value in the discriminator column.

Also destroy the Person object. Do this after you've evicted it, so that Hibernate doesn't try to delete the DB row.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 4:19 am 
Beginner
Beginner

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Thank you very much !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 4:25 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
If You want to avoid troubles, redesign Your system so that You do not need to change the type of objects. Like using Person-PersonRole pattern.

Gert


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