When I use the unit of work pattern (with JPA), I get an entity from a repository, modify it and save the modifications at the and of the unit of work implicitly to the database.
Now I wonder how to perform validation with the unit of work pattern. If I apply changes (from user input) to a domain object and validate after that, the values of that object would be changed even if validation fails. What is the way to avoid writing an object with illegal state to the database? Roll back the unit of work?
One of the goals of OOP is that objects watch over their own state, so that they never accept illegal values. For this approach I had to throw exceptions and it is the opposite of the bean validation (and spring validation) approach from above. But maybe it is simpler and better.
What validation strategy would you use (in domain driven design)?