-->
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: Automatically choose class to instantiate
PostPosted: Fri Jul 13, 2007 8:45 pm 
Newbie

Joined: Fri Jul 13, 2007 7:22 pm
Posts: 4
Hibernate version: 3

Hello,

I'm fairly new to Hibernate, so excuse me if I'm misinterpreting the functionality here.

I have a class Foo, with a subclass ExtendedFoo. I want both mapped to the same table using class/subclass tags in the hibernate-mapping. But I don't want to use a discriminator column (I don't need it)...I want the Hibernate to automatically instantiate Foo objects when I do a find(). But I still want it to accept ExtendedFoo objects for saves and updates. I've looked...but I can't seem to find an example of how to do this. Does anyone here know?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 10:07 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Hi leptogenesis

I don't understand why you don't want to use the discriminator column for this purpose. Its the ideal way to store multiple, closely related class definitions in the same table. Plus there's no requirement to expose the discriminator-value programatically.

>>I want the Hibernate to automatically instantiate Foo objects when I do a find().
If ExtendedFoo extends Foo then doing a find on anything Foo related will return Foo compatible instances.

>> But I still want it to accept ExtendedFoo objects for saves and updates
The mapping files will specify where/how ExtendedFoo is stored so hibernate will take care of it.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 10:17 pm 
Newbie

Joined: Fri Jul 13, 2007 7:22 pm
Posts: 4
Quote:
I don't understand why you don't want to use the discriminator column for this purpose. Its the ideal way to store multiple, closely related class definitions in the same table. Plus there's no requirement to expose the discriminator-value programatically.


I don't want to modify the table. And considering what I'm trying to do, I don't think Hibernate should force me to put a dummy value in my table which forces it to always instantiate a Foo (in fact, if I insert an ExtendedFoo, I would prefer to get a regular Foo out...)

Quote:
If ExtendedFoo extends Foo then doing a find on anything Foo related will return Foo compatible instances.


But if I only map the Foo class, an insert of ExtendedFoo will throw an exception.

Quote:
The mapping files will specify where/how ExtendedFoo is stored so hibernate will take care of it.


Indeed, I could always write another mapping for the class ExtendedFoo...but that would add redundancy to my code.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 3:16 pm 
Newbie

Joined: Fri Jul 13, 2007 7:22 pm
Posts: 4
Sorry to double-post, but I think this deserves an update.

I've tried the most elegant kludge I can think of - I wrote another mapping which maps ExtendedFoo to the same table. This lets me do inserts fine...but then when I do a find("from Foo ..."), Hibernate returns each found value twice - once as a Foo, and once as an ExtendedFoo. This does not seem to be reasonable behavior...is there something I'm doing wrong? My only other idea is to get rid of the second mapping convert all the ExtendedFoo objects into Foo objects before inserting them...but that's terrible because it makes my code run slower.

And please let me know if you think I should post more of my code...I'm still new to the forum and don't really know what the common practices are.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.