-->
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.  [ 2 posts ] 
Author Message
 Post subject: Polymorphic insert
PostPosted: Tue Nov 15, 2005 1:08 pm 
Newbie

Joined: Tue Nov 15, 2005 12:48 pm
Posts: 1
Hi all,

I would like to know if this is possible:

I've got the two following classes:

class A
class B extends A

...each one have a mapping against a different table:
A -> TABLE_A
B -> TABLE_B

and some columns are replicated on both tables... legacy DB :-(

I need to "move" an instance of class B to class A (that is, copy the
data from TABLE_B to TABLE_A).

I try this way: i've got an instance of class B (let's say 'b') and make
a call to a method insert(A a) in one of my DAOs passing 'b' as argument. The implementation of insert() just makes a save on the current Session
with this object as argument.

I think (but i must be wrong) that, giving that b is an A, and my insert
method receives it as an "A" object, Hibernate should, when persisting, apply the A mapping (an insert against TABLE_A)... (and then i would
be very happy)... but no way: Hibernate always treats b as a "B" object,
apply the B mapping, and insert against TABLE_B...

Any tips?





PS: Pardon for my English... ;-)


Top
 Profile  
 
 Post subject: oo
PostPosted: Tue Nov 15, 2005 2:12 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
This has nothing to do with Hibernate, that is OO language at play:
Declaration like
Public void insert(A a ) means that the method accepts A and all the subclasses of A, but at runtime the given object fully retains its true type, in your case: B.

If you want to copy B to A then BeanUtils class from Jakarta-commons may help:

Pubic A convert( B b ){
A a = new A();
BeanUtils.copyProperties( a, b);
Return a;
}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


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