I have an entity, that represent order sent by the customer , this order might be updated after some discussion with the customer on phone, but the initial order sent by the customer must be persisted without update. how i can persist same entity twice , is it efficient to use deep cloning. i have tried to detach the the entity in order for persistence context to persist a new one , but still the persistence context is updating the first entry.
-
Do you need to preserve all of the contents of the original order? Or just the contents of the updated order? – jharig23 Jun 19 '13 at 17:19
-
i want to preserve the original order as it is, and to be able to update when a discussion with the customer take place, anyway i found a class that copy the whole entity with its dependency. – Mohammed Falha Jun 20 '13 at 19:22
3 Answers
You can not persist one object twice in one session, so you need copy your order and save (persist) it again. hibernate copy object values into new object with new generated ID

- 1
- 1

- 201
- 1
- 2
- 9
That's an interesting question. I think the quickest solution would probably be to use a multi-part ID. The first part would be the original order number and then every change increments the second part of the key. In your code you'd just need to find the object, make sure it's detached, alter the second part of the key and then persist it. As long as it's been detached it should then be saved away as a new order.
This post shows you how to use a composite key.

- 1
- 1

- 4,083
- 7
- 37
- 48
You need to clone/copy the object, ensure it has a unique id (or null if generated).
In EclipseLink there is an API to copy objects,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup#Copy_Examples

- 17,965
- 11
- 91
- 146