I have two domain objects:
- User, which extends Person
I have an HTML form where the basic properties of a person (which includes a user) can be changed. This form is controlled by a SimpleFormController whose "commandClass" property is set to Person.class.
My onSubmit() method calls PersonService.update(Person), which sets some fields (last updated on, last updated by), then calls PersonDAO.update(Person). My DAO is implemented using Hibernate, and in case it's relevant, my Hibernate mapping file uses the "class hierarchy per table" approach, i.e. the "person" db table contains both Person and User objects.
This all works fine when the person being edited is an instance of Person. However if the person is a User, the DAO throws an exception with the message "Object with id: null was not of the specified subclass: Person (class of the given object did not match class of persistent copy)".
Some logging in the controller tells me that the command object passed to onSubmit(), and which I am passing on to the PersonService, is an instance of Person (not surprising, given that I configured the controller's command class to be Person.class). FWIW, at this point the Hibernate session contains a CGLIB-generated "User" proxy with the same ID as the user being edited. I'm using the OpenSessionInViewInterceptor if that makes any difference.
I'd like to know if there's a way to set up a SimpleFormController to handle this kind of class structure, i.e. so that it uses a command object that is of the same class as the object being edited, e.g. the command would be a Person object when a person was being edited, and a User object when a user was being edited. Or is this asking too much of SimpleFormController?
How do other people handle this scenario?