-->
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 - convert/update to child class
PostPosted: Wed Jan 27, 2010 8:16 pm 
Newbie

Joined: Wed Jan 27, 2010 8:09 pm
Posts: 5
Hi all,

I am looking for a bit of 'best practice' advice.

Currently I have the following mapped classes:
Code:
@Inheritance(strategy=InheritanceType.JOINED)
public class Account


Code:
@PrimaryKeyJoinColumn(name="account_id")
public class ParentAccount extends Account


In my system generally 'Accounts' will be saved. However at later dates Accounts may need to be converted to ParentAccounts as they need extra responsibilities that Accounts do not include.

In terms of best practice whats the best way to upgrade a persistent stored item to a subclass?

Would you have to remove the previous item then re-add the account? Or is there another more efficient way of doing this?

Many thanks to all who read I appreciate your help.

Eggsy


Top
 Profile  
 
 Post subject: Re: Inheritance - convert/update to child class
PostPosted: Thu Jan 28, 2010 4:43 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Note that in the object-oriented world it is not possible for an object to change from one class to another. My first approach would be to try to redesign the code to get rid of the superclass/subclass relationship. Each account could for example have an AccountType.

If you can't do that and still want to use Hibernate cloning might be possible. Eg. load the Account object that you want to convert and create a new ParentAccount object and then copy properties to the new object. Then, delete the original and save the new object. This can be a problematic approach if you have lots of other objects with references to the original Account since you will also need to changes those to reference the new ParentAccount.

As a last resort you may bypass Hibernate for this and use SQL directly against the database. Just make sure that the Account you are trying to convert doesn't exist in memory, eg. in a Hibernate session or in the second-level cache.


Top
 Profile  
 
 Post subject: Re: Inheritance - convert/update to child class
PostPosted: Thu Jan 28, 2010 6:18 am 
Newbie

Joined: Wed Jan 27, 2010 8:09 pm
Posts: 5
Hi there,

Thank you for the reply.

I think I may have to go for the last resort option of performing a simply insert into the parent_account table.

I have also looked into specifying:
Code:
@SQLInsert(sql="INSERT INTO parent_account (account_id) VALUES(?)"


Because the parent_account table only has the id (currently) so that is all I need to be inserted but I'm using Hibernate from within a Spring container and cannot seem to make it use my defined SQLInsert function.


Top
 Profile  
 
 Post subject: Re: Inheritance - convert/update to child class
PostPosted: Thu Jan 28, 2010 7:04 am 
Newbie

Joined: Wed Jan 27, 2010 8:09 pm
Posts: 5
Hi there,

A bit more information on this.

After rethinking my ORM I have implemented the ParentAccount table to reproduce the composite design pattern so that potentially any account can be linked to other 'accounts'

So really I should save everything as a ParentAccount and set the parentAccountId only in linked child entities.

I guess I'm saying my design/storage process was incorrect which is why I was getting stuck when trying to implement my saving mechanisms.


Top
 Profile  
 
 Post subject: Re: Inheritance - convert/update to child class
PostPosted: Thu Jan 28, 2010 7:05 am 
Newbie

Joined: Wed Jan 27, 2010 8:09 pm
Posts: 5
Hi there,

A bit more information on this.

After rethinking my ORM I have implemented the ParentAccount table to reproduce the composite design pattern so that potentially any account can be linked to other 'accounts'

So really I should save everything as a ParentAccount and set the parentAccountId only in linked child entities.

I guess I'm saying my design/storage process was incorrect which is why I was getting stuck when trying to implement my saving mechanisms.


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.