-->
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.  [ 5 posts ] 
Author Message
 Post subject: interesting problem
PostPosted: Thu Mar 16, 2006 11:01 am 
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 6:50 pm 
Beginner
Beginner

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


Top
 Profile  
 
 Post subject: Re: interesting problem
PostPosted: Fri Mar 17, 2006 12:54 am 
Beginner
Beginner

Joined: Tue Jun 07, 2005 8:24 am
Posts: 33
Location: Cincinnati, OH
What if you weren't using the table-per-hierarchy strategy? What would you expect Hibernate to do then? In Java, the Student object is not the same object as the Person object. Why would you expect it to be in the database?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 17, 2006 3:07 am 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
IMO, the right way to do it is :
delete Person
insert student


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

Joined: Tue Mar 07, 2006 8:50 am
Posts: 20
Quote:
IMO, the right way to do it is :
delete Person
insert student


That's what I was doing before ... but it's not very optimized..


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