users@jpa-spec.java.net

[jpa-spec users] Re: Partial rollback of entities/transactions

From: Christian Kaltepoth <christian_at_kaltepoth.de>
Date: Sat, 9 Feb 2013 08:29:02 +0100

Hey Sebastian,

in my opinion a JPA entity is a one-to-one representation of data in the
database. Therefore you should only modify properties of the entity, when
you want to change the values in the database.

Therefore it is IMHO not a very good practice to modify properties of an
attached entity although the user is still able to press a "cancel" button.

So in my opinion you should really go with "workaround #1" and simply
create copy of object to work on. That's a much cleaner solution and you
have more control over how changes are handled when the user either clicks
"OK" or "Cancel".

Just my 2 cents on this. :)

Christian



2013/1/30 <sheba.public+javanet_at_gmail.com>

> Hello everyone,
>
> I stumbled upon a problem that I think deserves a standard solution in
> JPA.
>
> Imagine an application that allows a user to update an entity (or a set
> of entities) in several successive steps (e.g. in separate dialogs)
> before committing to the database. Within each step the entity can be
> updated (one or several times) and the user has to see its current
> state. The user can choose to cancel a step (dialog) at any time and
> proceed with another, or restart the same step again.
>
> As far as I know (maybe I'm wrong) there no way to cancel only selected
> changes made to an uncommitted entity. So far I heard/read about two
> workarounds:
>
> 1. Before each step (dialog) you create a copy of the entity and work
> with it. If the step is finished gracefully the state of the copy has
> to be written to the entity. If the step is canceled the entity remains
> unchanged.
>
> 2. One needs to track all the changes within a step and manually undo
> the changes on the entity.
>
> In my opinion this use case should be quite common and deserves an easy
> high-level API. There are two similar mechanism I know about:
>
> A) Savepoints: JDBC offers the concept of savepoints within an
> Transaction to do a partial rollbacks.
> B) Nested transactions: Allows to create sub-transactions which can be
> rolled back separately.
>
> A similar construct in JPA would help a lot to avoid cumbersome,
> handmade, solutions.
>
> - Sebastian
>



-- 
Christian Kaltepoth
Blog: http://blog.kaltepoth.de/
Twitter: http://twitter.com/chkal
GitHub: https://github.com/chkal