-->
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: Inheritance for saving, not for loading
PostPosted: Mon Apr 19, 2004 3:37 pm 
Newbie

Joined: Mon Apr 19, 2004 3:24 pm
Posts: 9
HI all,

I'm attempting to persist a class heirarchy without really wanting to persist the heriarchy.... that is, during processing of incoming requests, I may be using subclasses, e.g: SpecialFoo, which inherits from Foo.

Foo itself has a mapping file. SpecialFoo does not.
Upon attempting to persist a SpecialFoo, I get "No persister for: SpecialFoo". Which is reasonable I suppose, since I have not defined a mapping for this class (but it does inherit from the concrete Foo).

So my Q is this: Is there a way in hibernate to:
1) Write derived classes to the DB (for example, a SpecialFoo), using it's base class mapped properties
2) Read object just using the base mapping file.

With (2) above, I realize that upon reading/loading objects I'd simply be instantiating Foo objects for every row in the table. This is fine. I just need to understand if it's possible to persist (save) derived classes without doing the "full on" descriminator style mapping (which is my simple case, would introduce descriminator values where in reality there are none - the descrimination occurs only in the objects during processing, not in the data they end up holding).

Regards,
Neil Clayton


Top
 Profile  
 
 Post subject: Re: Inheritance for saving, not for loading
PostPosted: Wed Oct 19, 2005 10:32 am 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
i am also looking for a solution to the same problem. it's a not uncommon to create sub-class of persistent class that represents extra business logic which has nothing to do with persistence. and it's not reasonable to configure each sub-class (effectively as an empty sub-class) in hibernate, because sometimes you don't even know what sub-classes can be created. they can be created by your users if you are selling a software product/API.

to solve your first problem of saving a sub-class instance, you can use
session.save(<base class name>, <sub class instance>)

what i am struggling with is the 2nd problem to have load/query instantiate a sub-class instance. i tried the interceptor approach and it doesn't appear to work.

can someone enlighten me on how to solve it? i played with extending the load API on Session a little bit. but i would like to know if there is already an easy way out, especially for the query result.

thanks.

scornflake wrote:
HI all,

I'm attempting to persist a class heirarchy without really wanting to persist the heriarchy.... that is, during processing of incoming requests, I may be using subclasses, e.g: SpecialFoo, which inherits from Foo.

Foo itself has a mapping file. SpecialFoo does not.
Upon attempting to persist a SpecialFoo, I get "No persister for: SpecialFoo". Which is reasonable I suppose, since I have not defined a mapping for this class (but it does inherit from the concrete Foo).

So my Q is this: Is there a way in hibernate to:
1) Write derived classes to the DB (for example, a SpecialFoo), using it's base class mapped properties
2) Read object just using the base mapping file.

With (2) above, I realize that upon reading/loading objects I'd simply be instantiating Foo objects for every row in the table. This is fine. I just need to understand if it's possible to persist (save) derived classes without doing the "full on" descriminator style mapping (which is my simple case, would introduce descriminator values where in reality there are none - the descrimination occurs only in the objects during processing, not in the data they end up holding).

Regards,
Neil Clayton


Top
 Profile  
 
 Post subject: Re: Inheritance for saving, not for loading
PostPosted: Wed Oct 19, 2005 10:39 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
weiqingh wrote:

....

what i am struggling with is the 2nd problem to have load/query instantiate a sub-class instance. i tried the interceptor approach and it doesn't appear to work.

can someone enlighten me on how to solve it? i played with extending the load API on Session a little bit. but i would like to know if there is already an easy way out, especially for the query result.

thanks.



How do you expect Hibernate to determine your subclass type if they're not mapped and your not using a descriminator ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Inheritance for saving, not for loading
PostPosted: Wed Oct 19, 2005 10:40 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
weiqingh wrote:

....

what i am struggling with is the 2nd problem to have load/query instantiate a sub-class instance. i tried the interceptor approach and it doesn't appear to work.

can someone enlighten me on how to solve it? i played with extending the load API on Session a little bit. but i would like to know if there is already an easy way out, especially for the query result.

thanks.



How do you expect Hibernate to determine your subclass type if they're not mapped and your not using a descriminator ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Inheritance for saving, not for loading
PostPosted: Wed Oct 19, 2005 10:56 am 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
well, just like the Session.save API, i am content with allowing users to specify the entity type for load and query. for example, we can add a new API to Session:

public void load(Object object, String entityClassName, Serializable id)

or

public Object load(Class theSubClass, String entityClassName, Serializable id)

i actually tried to implement the first API myself and got it to work with minimum change. but i am not sure about instantiating sub-class in query result. also i wonder if there is a better way doing it.

pksiv wrote:
weiqingh wrote:

....

what i am struggling with is the 2nd problem to have load/query instantiate a sub-class instance. i tried the interceptor approach and it doesn't appear to work.

can someone enlighten me on how to solve it? i played with extending the load API on Session a little bit. but i would like to know if there is already an easy way out, especially for the query result.

thanks.



How do you expect Hibernate to determine your subclass type if they're not mapped and your not using a descriminator ?


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.