-->
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.  [ 11 posts ] 
Author Message
 Post subject: Change the subclass of a persistent entity: is it possible?
PostPosted: Tue Apr 27, 2004 7:28 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
I know this doesn't make much sense but... is it possible to change the subclass type of an entity?

I.e.: can I manually change the value of the discriminator in the line for that entity? I tried creating a new instance and setting it the id of the object I want to replace, but since I previously needed to load the same object in the session, hibernate does complain that a different object with the same id was already loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 7:40 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
Note: the "object already loaded" problem can be solved by using session.evict, but it seems that the instance type is not saved on update...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 7:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can try to delete first a recreate with the appropriate type + forcing id (be carefull with cascade...)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 9:18 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
If you can't delete the object, you can try evicting it from the session, updating the discriminator using plain jdbc, then reloading the object.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 9:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Hibernate does not do anything you can't do in your java object model. Objects just don't suddenly change their type in java, and its just the same in Hibernate. I suggest you change your class model. This has been discussed in a lengthy thread sometime ago, search for it if you want more info.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 10:45 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
michael wrote:
Hibernate does not do anything you can't do in your java object model. Objects just don't suddenly change their type in java, and its just the same in Hibernate. I suggest you change your class model. This has been discussed in a lengthy thread sometime ago, search for it if you want more info.


I agree with you, it's absolutely reasonable: I had already decided it is better for me to change my model. Thanks anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 27, 2004 10:46 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
jfifield wrote:
If you can't delete the object, you can try evicting it from the session, updating the discriminator using plain jdbc, then reloading the object.


A bit too ugly as a solution, for me, but thanks anyway! ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 6:26 pm 
Newbie

Joined: Wed Sep 22, 2004 12:18 pm
Posts: 14
michael wrote:
Hibernate does not do anything you can't do in your java object model. Objects just don't suddenly change their type in java, and its just the same in Hibernate. I suggest you change your class model. This has been discussed in a lengthy thread sometime ago, search for it if you want more info.


I hear what you're saying, but it's not really a fair point since the Object and relational worlds have different limitations, and therefore need to different features in order to achieve the same functionality.

Namely, in the object world if I have B as sublcass of A, I can make a new instance of B, that has the same ID property as some existingin instance of A.

In the relational world, I simply can't add a new row to a table that has the same ID as an existing row, so I have to delete the "instance" of A before I can create a new instance of "B". This is why you need to be able to "change types" in the relational world, even though you can't do it in Java.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 4:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, do it in the relational world by using JDBC.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 27, 2005 1:36 pm 
Newbie

Joined: Wed Sep 22, 2004 12:18 pm
Posts: 14
yeah, but you also have to worry about the Hibernate caches, as someone pointed out earler, and you can't do that using JDBC...so some messy combination of jdbc update and hibernate evict calls is required.

how about allowing a "where" attribute in the subclass element, much like it exists in the class element? That way, the purists can keep their immutable discriminator while allowing a workaround using the where clause on an updatable property as the filter for the subclass. The one caveat would be that the where clauses in class and subclass could conflict ( e.g. A = foo, and A != foo ), but that could be considered user error.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 11:20 am 
Newbie

Joined: Fri Aug 20, 2004 10:56 am
Posts: 2
My take on this was to call:
session.evict(oldObject);
session.replicate(newObject, ReplicationMode.OVERWRITE);

Some information gets updated but not the discriminator, which really I think is a bug. The code seems to check the id to determine if an update or insert is required, but doesn't for an sql update for the discriminator column.


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