0

I have different types of users. Every users inherit from a class : AppUser. Some users have a subclass of AppUser : AdminUser.

Is it possible for an AppUser to become an AdminUser? And for an AdminUser to become an AppUser?

I've tried to do appUserInstance.class = my.package.AdminUser But ended up with a ReadOnlyPropertyException

I've also tried appUserInstance.properties = new com.michelin.dur.AppUser(appUserInstance.properties).properties -> object references an unsaved transient instance

So the best I could actually do was removing / recreating the object. However, that's still a problem, as all links pointing to my previous User instance will point to nothing.

Anyone having a good solution to that?

1 Answer 1

1

You can do it with HQL:

DomainClass.executeUpdate("update DomainClass dc set dc.class = '${newDomainClass.name}' where id = ${id}")

I'd advise against it though - HQL skips validation, so if the classes AppUser and AdminUser have different properties you might end up with invalid objects in the database when doing that.

I would try to solve this by using permissions (roles) or if that's too much, by using a property called UserType instead of inheritance.

If not, recreating the user doesn't seem bad - if the classes AdminUser and AppUser are different, the user instance should also be.

1
  • Are HQL queries rolledback too in transactionnal services? If so, can't I just change the class using HQL and then force it to validate by saving the object? I would just have to roll it back if there is an error. Jul 23, 2015 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.