TwinColSelect and EclipseLink causes CloneNotSupportedException with Set

Hi!

I have a problem with TwinColSelect (and ListSelect) and entity fields type java.util.Set. I’m using a Form (using field without form doesn’t change anything), which have a TwinColSelect (or ListSelect, dosen’t make difference). Form have following person entity as a datasource:

public class Person extends AbstractPojo {

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name="RELATIONSHIP", joinColumns=@JoinColumn(name="MANAGER_PERSON_ID"), 
			inverseJoinColumns=@JoinColumn(name="EMPLOYEE_PERSON_ID")) 
	@OrderBy("lastName, firstName")
	private Set<Person> employeePersons;
	
	public Set<Person> getEmployeePersons() {
		return employeePersons;
	}

	public void setEmployeePersons(Set<Person> employeePersons) {
		this.employeePersons = employeePersons;
	}


}

I’m trying to store new entity (database stored entities dosen’t have this problem) like this:
IFacade facade = FacadeFactory.getFacade();
facade.store(abstractPojo);

This throws exception:

20.2.2012 10:53:17 com.vaadin.Application terminalError
SEVERE: Terminal error:
com.vaadin.event.ListenerMethod$MethodException
Cause: javax.persistence.RollbackException: Exception [EclipseLink-6055]
 (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.QueryException
Exception Description: The method invocation of the method [protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException]
 on the object [[fi.festum.cqsystems.data.person.model.Person@c829]
], of class [class java.util.Collections$UnmodifiableSet]
, triggered an exception.
Internal Exception: java.lang.reflect.InvocationTargetException
Target Invocation Exception: java.lang.CloneNotSupportedException: java.util.Collections$UnmodifiableSet
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:532)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)
	at com.vaadin.ui.Embedded.fireClick(Embedded.java:520)
	at com.vaadin.ui.Embedded.changeVariables(Embedded.java:506)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1451)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1399)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1318)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:763)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
	at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:164)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:306)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:323)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1719)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)
Caused by: javax.persistence.RollbackException: Exception [EclipseLink-6055]
 (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.QueryException
Exception Description: The method invocation of the method [protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException]
 on the object [[fi.festum.cqsystems.data.person.model.Person@c829]
], of class [class java.util.Collections$UnmodifiableSet]
, triggered an exception.
Internal Exception: java.lang.reflect.InvocationTargetException
Target Invocation Exception: java.lang.CloneNotSupportedException: java.util.Collections$UnmodifiableSet
	at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
	at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
	at org.vaadin.appfoundation.persistence.facade.JPAFacade.store(JPAFacade.java:221)
	at fi.festum.util.data.common.dao.CommonDAO.store(CommonDAO.java:159)
	at fi.festum.cqsystems.data.common.dao.CqCommonDAO.store(CqCommonDAO.java:24)
	at fi.festum.cqsystems.data.person.dao.PersonDAO.store(PersonDAO.java:115)
	at fi.festum.cqsystems.view.maintenance.person.PersonEditView.onSaveClicked(PersonEditView.java:727)
	at fi.festum.cqsystems.view.component.BaseEditView$2.click(BaseEditView.java:97)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:512)
	... 28 more
Caused by: Exception [EclipseLink-6055]
 (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.QueryException
Exception Description: The method invocation of the method [protected native java.lang.Object java.lang.Object.clone() throws java.lang.CloneNotSupportedException]
 on the object [[fi.festum.cqsystems.data.person.model.Person@c829]
], of class [class java.util.Collections$UnmodifiableSet]
, triggered an exception.
Internal Exception: java.lang.reflect.InvocationTargetException
Target Invocation Exception: java.lang.CloneNotSupportedException: java.util.Collections$UnmodifiableSet
	at org.eclipse.persistence.exceptions.QueryException.methodInvocationFailed(QueryException.java:808)
	at org.eclipse.persistence.internal.queries.InterfaceContainerPolicy.invokeCloneMethodOn(InterfaceContainerPolicy.java:245)
	at org.eclipse.persistence.internal.queries.InterfaceContainerPolicy.cloneFor(InterfaceContainerPolicy.java:98)
	at org.eclipse.persistence.mappings.CollectionMapping.buildBackupCloneForPartObject(CollectionMapping.java:172)
	at org.eclipse.persistence.internal.indirection.IndirectionPolicy.backupCloneAttribute(IndirectionPolicy.java:74)
	at org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy.backupCloneAttribute(TransparentIndirectionPolicy.java:66)
	at org.eclipse.persistence.mappings.ForeignReferenceMapping.buildBackupClone(ForeignReferenceMapping.java:224)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildBackupClone(ObjectBuilder.java:503)
	at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.buildBackupClone(DeferredChangeDetectionPolicy.java:206)
	at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.revertChanges(DeferredChangeDetectionPolicy.java:270)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.resumeUnitOfWork(UnitOfWorkImpl.java:5205)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.synchronizeAndResume(UnitOfWorkImpl.java:5154)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.synchronizeAndResume(RepeatableWriteUnitOfWork.java:548)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1142)
	at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
	... 40 more
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.eclipse.persistence.internal.security.PrivilegedAccessHelper.invokeMethod(PrivilegedAccessHelper.java:402)
	at org.eclipse.persistence.internal.queries.InterfaceContainerPolicy.invokeCloneMethodOn(InterfaceContainerPolicy.java:240)
	... 53 more
Caused by: java.lang.CloneNotSupportedException: java.util.Collections$UnmodifiableSet
	at java.lang.Object.clone(Native Method)
	... 59 more

Not I have change my entity setter like following


	public void setEmployeePersons(Set<Person> employeeSet) {
		employeePersons = null; 
		if (employeeSet != null) {
			Iterator<Person> item = employeeSet.iterator();
			while (item.hasNext()) {
				Person tmp = item.next();
				addEmployeePerson(tmp);
			}
		}
	}
	
	public void addEmployeePerson(Person person) {
		if (employeePersons == null)
			employeePersons = new HashSet<Person>();
		employeePersons.add(person);
	}

In this case everything works just fine. Is this the right way to avoid this problem? I Have several similar cases and I have also @OneToMany relatinships having same kind of problems.