Reputation: 1161
Consider the following scenario where Jpa is used for persistence. A student can be associated to different courses with a web form. So this form displays different entities (student, course).
The Save button is pushed, the business logic modify some fields of the entities, but the db operation fails.
Unfortunately the enities in memory reflects the changes made by the business logic and this may create some inconsistency problem. Is there a pattern useful in similar scenarios ?
Possible solution I thought and why I don't like them:
Otherwise I can clone the entities, make the changes and swap the clone with the original entity after a successful commit.
Anyway I would be more confortable following a well established pattern.
Upvotes: 2
Views: 234
Reputation: 470
The Memento Pattern is a design pattern intended to offer 'rollback' functionality for the state of objects in memory. You need a Caretaker class, that asks the subject for a Memento, then attempts the persistence. The Caretaker would then give the Memento back to the subject, telling it to roll back to the state the Memento describes, if necessary.
Upvotes: 1