Most popular web site - Conversations with JPA returned Item, mergedItem, is a
Conversations with JPA returned Item, mergedItem, is a different instance! The client now has two Item objects: the old one and the new one. As we pointed out in Merging the state of a detached object in section 9.3.2, the reference to the old instance should be considered obsolete by the client: It doesn t represent the latest state. Only the mergedItem is a reference to the up-todate state. With merging instead of reattachment, it becomes the client s responsibility to discard obsolete references to stale objects. This usually isn t an issue, if you consider the following client code: ManageAuction controller = new ManageAuction(); // First event Item item = controller.getAuction( 1234l ); // Item is displayed on screen and modified… item.setDescription(”[SOLD] An item for sale”); // Second event item = controller.endAuction(item); The last line of code sets the merged result as the itemvariable value, so you effectively update this variable with a new reference. Keep in mind that this line updates only this variable. Any other code in the presentation layer that still has a reference to the old instance must also refresh variables be careful. This effectively means that your presentation code has to be aware of the differences between reattachment and merge strategies. We ve observed that applications that have been constructed with an extended persistence context strategy are often easier to understand than applications that rely heavily on detached objects. 11.3.3 Extending the persistence context in Java SE We already discussed the scope of a persistence context with JPA in Java SE in chapter 10, section 10.1.3, Transactions with Java Persistence. Now we elaborate on these basics and focus on examples that show an extended persistence context with a conversation implementation. The default persistence context scope In JPA without EJBs, the persistence context is bound to the lifecycle and scope of an EntityManager instance. To reuse the same persistence context for all events in a conversation, you only have to reuse the same EntityManager to process all events. An unsophisticated approach delegates this responsibility to the client of the conversation controller:
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.