I am experimenting with JPA in standalone mode (backed by Hibernate).
I’ve noticed that the following code succeeds in changing the database value to “garbled”:
A a = em.find(A.class, 1);
a.setA1("garbled");
entityTransaction.begin(); // line 3
entityTransaction.commit(); // line 4
em.close();
There is no other transaction begin() before line 3.
The language used in the API spec for EntityTransaction would seem to indicate otherwise or perhaps something is a bit counter-intuitive in the model of demarcating a JPA entity transaction with begin() and commit()? Note that lines 3 and 4 are necessary as commenting them out does not change the database value.
ain your example is a managed entity associated witheminstance, can be modified outside a transaction boundary and will be persisted as part of committing a transaction from the same entity manager instance. The transaction boundaries only define what goes into the database when the transaction is committed and what changes made to the DB get reverted when the transaction fails. Assuming that you failed the transaction in your example,awould still have the changes made, it is only that the database will not have these changes and so are not visible to others.If you were expecting that managed entities should not be modifiable outside the transaction boundaries, or any changes to them outside the transaction boundaries shouldn’t get persisted, then that is not true. The life cycle of a managed entity is tied with its entity manager.