Use a MANUAL flushmode conversation
I tried that one as well but it fails for this use case :
-> New Questionnaire
----> New Question (1)
----> Save Question (1)
----> Edit Question (1)
----> Cancel changes on Question 1
-> Save Questionnaire (em.flush())
I got the changes done during the edit step.
Thanks for the reply.
Ok no one see a solution without cloning ?
It would help me a lot !
Try using nested conversations I think, you may also need use a two entities, one for the parent and one for the nested conversation, and be careful when loading entities. You'll still need manual flush mode. An alternative would be manually roll back changes on cancel, or to use detached entities. I pretty sure cloning isn't necessary.
Thank for your reply Pete.
I tried nested conversation without success. The problem is that you get the same persistence context even if you use two components.
I didn't find a solution with JTA as well :
- Start a transaction with entity manager 1
- Persist the parent
- Start a transaction with entity manager 2
- merge the parent in the new persistence context
- create a child
- add it to the parent
- persist the child
- commit the transaction
- close entity manager 2
- rollback transaction (EM 1)
=> parent is rolled back but the child is in the database because of the commit in the nested transaction.
Do you mean something different by "roll back changes on cancel" ?
How can you use detached entities to solve this problem ?
Ooops, sorry, I should have written
Try using nested conversations I think, you may also need use multiple entitiy managers, one for the parent conversation and one each nested conversation - be careful when loading entities that you get it from the correct one.
This is probably best, but is perhaps hardest to get right IMO.
You don't want to do this using transaction AFAICS, I really meant you would do a reload of the entity from the PC in code, but I this is really fighting against the way hibernate works and would be a mess!
Detached entities would allow you to manually merge only the entities you wanted to save (along with the correct cascades) - but you loose all the benefits of the entity being managed. Perhaps this is most practical solution.