-->
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.  [ 11 posts ] 
Author Message
 Post subject: subclass instance from existing superclass
PostPosted: Mon Nov 08, 2004 9:02 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Dear tribe,

I'm trying to figure out how to create an instance of a subclass for an instance of a superclass that already exists.

Consider two subclasses, Human and Woman (a joined-subclass of Human). Now, suppose i have a human record with 1 as the primary key. I tried creating the subclass instance like:

Code:
// get the superclass instance first
Human human = sess.load(Human.class, humanId);

// now create the subclass instance
Woman woman = new Woman();
woman.setSomething(something);

// try to save the woman
sess.save(woman, human.getId());


but the final line results in

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

my problem is that i have to create this subclass instance while the superclass instance is already there. Is there a way to do that or should i try to look at alternative ways to map this?

Thanks for your time and help,

Manos


Top
 Profile  
 
 Post subject: Re: subclass instance from existing superclass
PostPosted: Mon Nov 08, 2004 9:14 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
Why not make superclass abstract?

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 09, 2004 5:16 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Hi ko5tik,

Thanks for your reply.

Well, because Human is used in an other part of the application as a concrete class. I later have to use a Human record as an instance of Woman by extending it (add additional properties etc) and keeping any relationships with the original record intact. Essentially, i just want to take advantage of the OO semantics and the joined subclass infastructure to extend a record.

Thanks for any pointers,

Manos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 09, 2004 5:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, this is not possible. Either delete Human first, use JDBC or rethink your object model. Objects can not just change their type - this has been discussed extensively already in the forum.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 09, 2004 5:31 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Hi Michael,

Thanks for your help and time (you saved a lot of mine ;-)

Essentially, I wanted to extend the functionality of a record for a new component of the application and thought it may be possible; after all, it would just be an FK from the Woman to Human, as it will become now (as a normal one-to-one).

I'm currently in the design process of another application that may require somethng like this; that is, discover at some point in time that a certain instance of Foo, actually is an instance of a subclass of Foo and the additional properties/information is available (to add the joined record). Do you think this is doable/desired? If so i will submit this on Jira and help as i can along the way.

Thanks again,

Manos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 09, 2004 6:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't think this will be implemented, as this is not possible either in the obejct-world (you can't just change an objects class). I'd suggest if you design an application that needs such a feature, not to use subclassing but aggregation for it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 25, 2004 12:41 pm 
Beginner
Beginner

Joined: Sun Feb 08, 2004 4:09 pm
Posts: 46
In object-world you can't change an object type, althought an object may be specialized sometimes, ie:

Employee
/\
|
|
Supervisor

Where employee is not abstract and supervisor has some overwritten behaviors, so, an employee may become a supervisor...

Cya!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 25, 2004 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I'd really like to see how you do that:

Object o = new Supervisor();

... do some magic

(o instanceof Supervisor) == false;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 25, 2004 8:34 pm 
Beginner
Beginner

Joined: Sun Feb 08, 2004 4:09 pm
Posts: 46
Well,

Instantiate new Supervisor, copy Emplyee's information, fill-in with aditional information that supervisor may need, keep same id, persist the new object...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 25, 2004 9:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Thats not the same object! you can do just that with hibernate: instantiate new object, copy things over, delete old one, persist new one.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 26, 2004 11:58 am 
Beginner
Beginner

Joined: Sun Feb 08, 2004 4:09 pm
Posts: 46
Hmm, yeh, i understand what you're saying...


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