Repository: phac-nml/irida
Issue: Synchronizing projects with metadata does not work

In our latest `development` branch, if I try to sync a project where samples have metadata assigned (e.g., derived from a pipeline) I will get the following exception:

```
14 Dec 2018 13:40:06,639 DEBUG ca.corefacility.bioinformatics.irida.service.remote.ProjectSynchronizationService:151 - An error occurred while synchronizing project http://localhost:8080/api/projects/3
org.springframework.orm.ObjectRetrievalFailureException: Object [id=null] was not of the specified subclass [ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry] : class of the given object did not match class of persistent copy; nested exception is org.
hibernate.WrongClassException: Object [id=null] was not of the specified subclass [ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry] : class of the given object did not match class of persistent copy
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:294)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
        at com.sun.proxy.$Proxy161.save(Unknown Source)
        at ca.corefacility.bioinformatics.irida.service.impl.CRUDServiceImpl.update(CRUDServiceImpl.java:223)
        at ca.corefacility.bioinformatics.irida.service.impl.sample.SampleServiceImpl.update(SampleServiceImpl.java:172)
        at ca.corefacility.bioinformatics.irida.service.impl.sample.SampleServiceImpl.update(SampleServiceImpl.java:67)
        at ca.corefacility.bioinformatics.irida.service.impl.sample.SampleServiceImpl$$FastClassBySpringCGLIB$$9792ab2e.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:718)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:654)
        at ca.corefacility.bioinformatics.irida.service.impl.sample.SampleServiceImpl$$EnhancerBySpringCGLIB$$89119358.update(<generated>)
        at ca.corefacility.bioinformatics.irida.service.remote.ProjectSynchronizationService.syncSample(ProjectSynchronizationService.java:287)
        at ca.corefacility.bioinformatics.irida.service.remote.ProjectSynchronizationService.syncProject(ProjectSynchronizationService.java:243)
        at ca.corefacility.bioinformatics.irida.service.remote.ProjectSynchronizationService.findMarkedProjectsToSync(ProjectSynchronizationService.java:143)
        at ca.corefacility.bioinformatics.irida.config.services.scheduled.ProjectSyncScheduledTaskConfig.syncProject(ProjectSyncScheduledTaskConfig.java:27)
...
```

Changed files:
src/main/java/ca/corefacility/bioinformatics/irida/model/sample/metadata/MetadataEntry.java
src/main/java/ca/corefacility/bioinformatics/irida/service/remote/ProjectSynchronizationService.java

diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/model/sample/metadata/MetadataEntry.java b/src/main/java/ca/corefacility/bioinformatics/irida/model/sample/metadata/MetadataEntry.java
index 9878c7d751..5d602d9a53 100644
--- a/src/main/java/ca/corefacility/bioinformatics/irida/model/sample/metadata/MetadataEntry.java
+++ b/src/main/java/ca/corefacility/bioinformatics/irida/model/sample/metadata/MetadataEntry.java
@@ -54,7 +54,11 @@ public class MetadataEntry {
 	public void setValue(String value) {
 		this.value = value;
 	}
-
+	
+	public void setId(Long id) {
+		this.id = id;
+	}
+	
 	public Long getId() {
 		return id;
 	}
diff --git a/src/main/java/ca/corefacility/bioinformatics/irida/service/remote/ProjectSynchronizationService.java b/src/main/java/ca/corefacility/bioinformatics/irida/service/remote/ProjectSynchronizationService.java
index d2dd424501..13e3c8d0af 100644
--- a/src/main/java/ca/corefacility/bioinformatics/irida/service/remote/ProjectSynchronizationService.java
+++ b/src/main/java/ca/corefacility/bioinformatics/irida/service/remote/ProjectSynchronizationService.java
@@ -361,6 +361,9 @@ public class ProjectSynchronizationService {
 	 */
 	public Sample syncSampleMetadata(Sample sample){
 		Map<String, MetadataEntry> sampleMetadata = sampleRemoteService.getSampleMetadata(sample);
+		
+		sampleMetadata.values().forEach(e -> e.setId(null));
+		
 		Map<MetadataTemplateField, MetadataEntry> metadata = metadataTemplateService.getMetadataMap(sampleMetadata);
 		sample.setMetadata(metadata);