Reputation: 49215
I am using CDI conversation scoped, Seam managed extended persistence context (PC). This provides more fine-grained control over PC and avoids LIEs. I am using CDI Beans instead of EJB Beans. In a page, I retrieve list of entities and show them in a table. The selected entity record from the table is binded to the form and can be edited but not persisted until the "save" button is clicked. In this place the problem occurs, since all entities are managed the edited entities are also persisted when I try to flush/commit only one current entity in save action. What is the preferred best-practice approach to this kind of problems. Should I use middle POJO between the managed entity and the viewed/edited one? Should I detach then merge (before saving) the current in-progress entity? Any suggestions?
JPA 2.0, Hibernate 4.x
Seam 3 (Weld CDI, persistence, transaction, faces modules)
JSF 2.1
Java EE 6.
Upvotes: 4
Views: 1311
Reputation: 49215
I am using Primefaces in front end and it was ajax submitting by default the changes made to the entity. The problem gone away after I have managed the Primefaces components.
Upvotes: 0
Reputation: 3504
We used a similar approach but with EJB3 beans and without the Seam persistence context. Nevertheless, maybe our experience could be useful to you.
The idea was :
extended
PersistenceContext
in the beanDetaching the entities will kill the benefit of giving entities to the front, as you will throw LazyException
each time you're trying to access something that wasn't loaded. It would be quite the same as use some DTO.
Hope it helps!
Upvotes: 3
Reputation: 47253
If i understand correctly, then:
I think the right approach here would be to detach the objects when they are edited, and then reattach them (by merging) when they are saved. That keeps the unsaved changes in memory only.
You don't actually need an extended persistence context for this, because any objects you need to keep between requests will be detached.
Upvotes: 2