I am experimentig with JPA and I have difficulties to reproduce an example taken from JavaMagazin 9/09 about the ECB Pattern.
JBoss 5.1.0 GA, MySQL 5.0
The "boundary" (facade)
The Warehouse Bean persists the order. This works like expected:
However the DeliveryService modifies the order:
Since the transaction should be still running, I expect that order is still in the persistence context and that the changes made by the DeliveryService should be automatically persisted. However they never arrive in the database.
Do I get the whole concept or persistence context wrong? Or is there some bug in my code?
Seems the checkout method is bugged if you are using it with a brand new Order instance: when using the merge method of the EntityManager with a new entity instance, this instance remained detached while a new managed instance is created and stored in the persistence context. It is this managed instance that is returned from the merge call. To get a hold on it, use the following code instead:
Then the Order instance returned from the checkout method is managed, and any change done by the deliver method will produce an sql update when the transaction commits.