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: Dynamically modifying mapped instance class to superclass
PostPosted: Fri Aug 15, 2008 11:45 am 
Newbie

Joined: Fri Aug 15, 2008 10:31 am
Posts: 2
Let's say I have three classes:

class A { ... }
class B extends A { ... }
class C extends A { ... }

They are mapped to DB using Hibernate's JOINED strategy.

B myB = new B();

When I persist myB, I have A's attributes placed into table "A", and B's attributes placed into table "B".

Now I want to change myB class to A, which actually means I want to clean table "B" of it's datarow (and all datarows in tables that FK-ed on table B). I cannot just remove this instance from DB and then put a new instance of class A, because I also have lots of other tables with FK restrictions on table "A", and I don't want this restrictions to be broken or cascaded some way.

So, what I need is:

1) persist "myB", now I have a new datarow in table "A" with PK; and a new datarow in table "B", which has it's own PK and also a FK to "A"
2) I fill some other tables which may have FK to A's or to B's PK
3) I want to change "myB" class to A, so I need datarow in table "B" deleted, along with all datarows that FK-ed to it (that means which FK-ed to B's PK). And *IT'S IMPORTANT*, that A's datarow and all datarows in other tables that FK-ed on A's PK must remain!
4) I want to change "myB" class to C, so I need to insert datarow in table "C" with new PK and a FK on A's PK

How could this be accomplished?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 15, 2008 12:22 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
This really sound like design mistake, because an Object should never change it's class.

I don't know what the real world reason for this desire is, but I try to give some ideas for a potential solution anyway.

How about this setup


Code:
class A{
Set<Role> roles;// a Map might be better here, or  a single instance

public <R extends Role> R getRole(Class<R> roleClass){
...
}
}

@inheritance(TablePerConcreteClass)
abstract class Role{
A theA
....}

class B extends Role{...}

class C extends Role{...}



This way every A can provide a B or C orboth, and everything can be mapped with standard Hibernate mappings.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 18, 2008 8:08 am 
Newbie

Joined: Fri Aug 15, 2008 10:31 am
Posts: 2
Thanks, your idea was just what I needed.


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.