users@jpa-spec.java.net

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

From: <sheba.public+javanet_at_gmail.com>
Date: Wed, 30 Jan 2013 15:00:48 +0000 (GMT)

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