I feel like I may be missing something. Due to hibernate's success I am sure that my thinking must be flawed, when it comes to this automatic dirty checking process.
I am using the Springframework with Hibernate 3, and I am using a SessionPerRequest pattern. So any object that I retrieve through load(),find(), etc... the object is in a persistant state, therefore any changes to an entities properties are automatically written to the database, cool huh!
Well not so cool if I want to have a validator validate the object before performing the update. Kind of like this:
Code:
// Hibernate DAO
// calls load(Client.class,id) ;
Client cli = clientDAO.getFromId(id);
ClientValidator validator = new ClientValidator() ;
cli.setName("New Name");
if( validator.validateClient(cli) ) {
// do something
// store client info
clientDAO.update(cli) ;
} else {
// inform system invalid client
}
The problem is that the information is already changed by the time validator is called. Should I be rolling back, if the validator says it isn't a valid client.
This seems like it would generate uneccessary SQL statements for validation that I can perform at the code level.
Any suggestions would be greatly appreciated. Thank you.