4

The Java EE 5 version of Entity Manager doesn't have a detach method, but I need this exact functionality. Is there a way to detach an entity in JPA 1.0, or is this functionality impossible to do?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • `clear()` if you can handle all of them being detached. – Sotirios Delimanolis Nov 12 '13 at 03:30
  • @SotiriosDelimanolis I'm worried that will have side effects that will take a long time to discover. Is there no way to detach a single one in JPA1.0? – Daniel Kaplan Nov 12 '13 at 03:31
  • 1
    If your object is in session, then it is attached. Once session closes you object will be detached. Are you looking for detach object from live session? – Chowdappa Nov 12 '13 at 03:33
  • @CHowdappaM Yes, I'm in a live session, but that is good information to know. – Daniel Kaplan Nov 12 '13 at 03:38
  • @CHowdappaM: Let me ask another question that would alleviate my concerns: I've got an entity which has a List of other entities. I want to modify that list but I don't want to persist any modifications back to the database. If I get the List then wrap it in a `new ArrayList(list);` and then use the result, will that protect me? – Daniel Kaplan Nov 12 '13 at 03:42
  • 1
    @tieTYT, NO. Can you try by cloning. – Chowdappa Nov 12 '13 at 03:46
  • 1
    Unfortunately 1.0 is only able to detach all as Sotirios said. They introduced single detachments in 2.0 – dispake Nov 12 '13 at 04:13
  • You could serialize/marshal the object and use the deserialized/unmarshalled version - it will be detached. – kostja Nov 12 '13 at 07:53
  • I use Dozer mapper to quickly copy the data from an entity into a 'DTO Class' so that I don't have to worry about unintended side effects. – Bill Rosmus Nov 01 '14 at 23:50

0 Answers0