Hello,
What I have in my application is a class, say Account, and a subclassed class AdminAccount. Sometimes I need to promote an Account to an AdminAccount. So I wanted to know if there is an idiomatic or good practice way to achieve that in Hibernate. The only solution I have found so far is to delete the old object and create a new one of the new class. I am not very happy with that as it seems ugly, and in particular, it forces me to copy all the elements of the collection of the Account.
So, is there a magic cast in Hibernate? :) Or is this the only way?
Note the following code below is Groovy code, not Java, but the problem is the same in Java.
Hibernate version: 3.2.6 (latest)
Code between sessionFactory.openSession() and session.close():
def hibernateSession = sessionFactory.getCurrentSession() Account user = (Account) hibernateSession.load(Account.class, myId);
AdminAccount account = new AdminAccount(user)
// We have to copy the collection, this is ugly user.articles.each() { account.addToArticles(it) } hibernateSession.delete(user) hibernateSession.flush() hibernateSession.save(account)
Name and version of the database you are using:
MySQL 5.0.40+
|