jquery

gist JS

Friday, December 01, 2006

Changing cats into dogs

Hibernate Modifying subclass's discriminator value on update

Well, despite the naysayers above I've succeeded in doing the unnatural and have changed the type of a polymorphic Hibernate class.

Dog changeCatToDog(Cat cat){
int res = (Integer) getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session sess) throws Exception {
String hqlUpdate = "update Animal set discriminator = 'dog' where animal_id = :id";
int updatedEntities = sess.createQuery( hqlUpdate )
.setLong( "id", cat.getId() )
.executeUpdate();
return updatedEntities;
}});

getHibernateTemplate().evict(t);

return (Dog) getHibernateTemplate().get(Dog.class, cat.getId());
}

No comments: