-->
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.  [ 4 posts ] 
Author Message
 Post subject: Modifying subclass's discriminator value on update
PostPosted: Wed Jul 07, 2004 6:06 pm 
Newbie

Joined: Wed Jul 07, 2004 5:48 pm
Posts: 2
Hi,

I am having difficulty performing updates on some persisted objects. In my situation, I have an abstract class(call it "Animal") which is extended by three concrete classes(call them "Cat", "Dog", and "Horse"). Niether Cat, Dog, nor Horse contain any fields of their own; the only thing that differs between them and Animal is the functionality of their methods. The hbm file for Animal looks something like this:

Code:
<hibernate-mapping>
   <class name="Animal" table="animals">
      <id name="id" type=... 

      <subclass name="Cat"
         lazy="true"
         discriminator-value="Cat">
      </subclass>
      <subclass name="Dog"... </subclass>
      <subclass name="Horse"... </subclass>
   </class>
</hibernate-mapping>


I would like to be able to load a Cat object, modify some of its fields, and then update it such that it gets persisted as a Dog object(ie, change the discriminator value). When I load the Cat object; copy all of its fields into a new dog object; modify some of those fields; and then update the database with this Dog object, the fields get correctly modified but the object is still stored as a Cat. In other words, Hibernate has refused to touch the discriminator-value of an object upon updating it.

So my question is: is it possible to modify a persisted object's class type and then update it, and have this change be reflected in the discriminator column?

Thank you,
Chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2004 1:02 pm 
Newbie

Joined: Wed Jul 07, 2004 5:48 pm
Posts: 2
I hate to be a nuissance, but if anyone could give me a simple 'yes' or 'no' answer as to whether this is possible it would be greatly appreciated.


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

Joined: Wed Sep 22, 2004 12:18 pm
Posts: 14
I have the same need, and the answer is NO, unfortunately...

one work around is to have your Animal class mapped to the table without using the discriminator ( i.e. reads all from table ) and then you can read a dog row and change it to cat, all via the Animal class.

once the updat is done, you should be able to read that row back as Cat, which will have its own ( duplicate ) mapping file to the same table, with a discriminator based on its type.

Hope that helps.


Top
 Profile  
 
 Post subject: success
PostPosted: Fri Dec 01, 2006 3:10 pm 
Newbie

Joined: Wed Mar 29, 2006 8:42 pm
Posts: 7
This isn't correct, it's possible. I just succeeded in doing this. Don't mind the Spring stuff. The real trick is the evict();

Code:
Dog changeCatToDog(Cat cat){
int res = (Integer) getHibernateTemplate().execute(new HibernateCallback(){
         public Object doInHibernate(Session sess) throws HibernateException, SQLException {
            String hqlUpdate = "update Animal set discriminator = 'dog' where animal_id = :id";
            int updatedEntities = sess.createQuery( hqlUpdate )
            .setLong( "id", cat.getId() )                              
            .executeUpdate();            
            return updatedEntities;            
         }});
      System.out.println("res: "+res);
      
      getHibernateTemplate().evict(t);
      
      return (Dog) getHibernateTemplate().get(Dog.class, cat.getId());
}


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