Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Help on clone method(clone)
Help on clone method [message #873076] Thu, 17 May 2012 13:15 Go to next message
Tim MacEachern is currently offline Tim MacEachernFriend
Messages: 1
Registered: July 2009
Junior Member
I'm working with an EclipseLink 2 application that is exhibiting a problem with duplicated records. I think I have tracked the problem down to a clone method in one of our Entities. This clone() clears the id itself, rather than having the calling routine do it. I suspect that EclipseLink is cloning an object for a new unit of work and a duplicate is being created at that time. Since the id has been cleared, the work object gets saved as a new persistent object. Unfortunately, my knowledge of EclipseLink is not deep enough to back up this assumption. Could someone more knowledgeable tell me whether a clone method like this would cause the problem I'm seeing, and whether the quick fix of taking out the id clearing (and compensating in client code) is all I need?

public SomeEntity clone() throws CloneNotSupportedException {
SomeEntity clone = (SomeEntity) super.clone();
return clone;

Finally, how could I go about testing this scenario?

Re: Help on clone method [message #873245 is a reply to message #873076] Thu, 17 May 2012 19:48 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1376
Registered: July 2009
Senior Member
Simplest test is to print off the stack when it gets called - (new RuntimeException()).printStackTrace(), or use a breakpoint in a debugger.

EclipseLink will use its own methods to create backups and working/managed instances, which involves creating a new instance and populating it with the required attributes. This is configurable though through a copy policy described here:
with the default being the InstantiationCopyPolicy. If JPA method access and weaving is used, the default is a PersistenceEntityCopyPolicy which uses a _persistence_shallow_clone() method.

I've found duplicates can occur when objects are not correctly persisted/merged, but that probably doesn't help track down how to find them. Adding debugging to your entities themselves and turning EcliseLink logging to finest so you can see where they might get inserted could help. I believe EclipseLink will print off the entity as persist/merge gets called on it at the finest log level.

Best Regards,
Previous Topic:stackOverFlowError
Next Topic:how to use two @OneToMany and @ManyToOne to replace @ManyToMany
Goto Forum:

Current Time: Fri Jan 22 20:23:20 GMT 2021

Powered by FUDForum. Page generated in 0.01642 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top