I'm introducing JPA in an existing desktop application with a persistence layer where objects are extracted from the DB and stored back using plain queries.
After an object is extracted, it is used in the UI side to show it and to keep the user edits. When the user press the "save" button the persistence layer is invoked passing the object and it is stored back in the DB. If the user press the "cancel" button the modification are throw away and nothing happens in the persistence layer.
I'm replacing the plain query mechanism with JPA. The problem now is that when an object is retrieved using JPA it is managed. When the user modify it, the changes are stored directly in the DB.
I can't modify the UI code so the new persistence layer (JPA based) should emulate the old persistence layer (plain query based) behaviour.
How can I obtain it?
I was thinking to detach the objects before returning them from the persistence layer. In this case the user modifications are not persisted until the persistence layer is invoked.
In order to detach the object I can:
- invoke the detach method in the EntityManager. This solution requires to annotate the relationships to get related object detached.
- clear the EntityManager after each persistence layer operation.
Is the detach solution right? Which detach method is better?