-->
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.  [ 3 posts ] 
Author Message
 Post subject: Persist subclass instance with superclass mapping
PostPosted: Mon Dec 04, 2006 2:08 am 
Newbie

Joined: Mon Dec 04, 2006 1:50 am
Posts: 2
Hi,

I am facing a problem related to Hibernate Mapping.

I have the following data beans:

Code:
public class A
{
    int id;
    String name;
}

public class B extends A
{
    String address;
}


I have mapped class A with table A.

Now, I have something like:

Code:
A obj = new B();


I want to persist obj in table A (only id and name) without defining a mapping for class B. I want the extra data in obj (address) to be discarded.

If I write session.save(obj), it gives me an error saying that there is no mapping for class B.

Is it possible to persist obj without defining a mapping for B?

Regards,
Jayarama Nettar.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 6:26 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
I dont think that this is possible with hibernate.
The reason may be something like:

hibernate does not throw information away when persisting instances.
In your case, obj is instance of B and this information
would be discarded.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 3:45 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 11:52 am
Posts: 43
I don't know if this would work, but here is my idea.

Define an interface, call it I, that A implements. Define the hibernate mapping for the interface only. Define only those properties that you want saved.

The problem I see is how would Hibernate know which concrete class to instantiate when it loads an object from the table?

It's not really that difficult to create mappings that have inheritance relationships. That way you can still store A and B in the same table and retrieve them as instances of the correct class. As long as you define no extra properties for B it will be saved with only the properties it inherits from A.

Good luck. Don't forget to rate.


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