title: Hibernate :"detached entity passed to persist" error
body: <p>I can not understand what's wrong in my code, in practice, in my system I want to make an assessment for a specific curriculum, I would like to save memory this assessment, but, when I try to save it, the system take me this error:</p>
  
  <p>complete stacktrace.</p>
  
  <pre><code>GRAVE: 
     Servlet.service() for servlet [Spring MVC Dispatcher Servlet] in context with path          [/orion-web] threw exception [Request processing failed; nested exception is    org.springframework.webflow.execution.ActionExecutionException: Exception thrown executing [AnnotatedAction@165b124 targetAction = [EvaluateAction@150c296 expression = inserimentoRecruitingController.salvaRecruiting(), resultExpression = [null]], attributes = map[[empty]]] in state 'valutazione' of flow 'valutazioneCandidato-flow' -- action execution attributes were 'map[[empty]]'] with root cause
  org.hibernate.PersistentObjectException: [color=#FF0000]detached entity passed to persist: it.dstech.dms.cigno.jpa.model.impl.CurriculumVitaeEntityImpl[/color]
     at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127)
     at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
     at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
     at org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
     at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
     at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
     at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
     at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
     at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
     at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
     at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
     at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
     at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
     at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
     at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
     at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
     at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
     at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
     at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
     at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
     at $Proxy30.persist(Unknown Source)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
     at $Proxy30.persist(Unknown Source)
     at it.dstech.dms.cigno.service.core.service.impl.recruiting.RecruitingServiceImpl.saveRecruiting(RecruitingServiceImpl.java:55)
     at it.dstech.dms.cigno.service.core.service.impl.recruiting.RecruitingServiceImpl$$FastClassByCGLIB$$1e5f6a9c.invoke(&lt;generated&gt;)
     at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
     at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:617)
     at it.dstech.dms.cigno.service.core.service.impl.recruiting.RecruitingServiceImpl$$EnhancerByCGLIB$$6a801cce.saveRecruiting(&lt;generated&gt;)
     at it.dstech.dms.orion.swf.manager.impl.GestioneRecruitingWFManagerImpl.doInsertRecruiting(GestioneRecruitingWFManagerImpl.java:45)
     at it.dstech.dms.cigno.model.InserimentoRecruitingController.salvaRecruiting(InserimentoRecruitingController.java:38)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:69)
     at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:110)
     at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
     at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
     at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:97)
     at org.springframework.binding.expression.spel.SpringELExpression.getValue(SpringELExpression.java:84)
     at org.springframework.webflow.action.EvaluateAction.doExecute(EvaluateAction.java:75)
     at org.springframework.webflow.action.AbstractAction.execute(AbstractAction.java:188)
     at org.springframework.webflow.execution.AnnotatedAction.execute(AnnotatedAction.java:145)
     at org.springframework.webflow.execution.ActionExecutor.execute(ActionExecutor.java:51)
     at org.springframework.webflow.engine.support.ActionTransitionCriteria.test(ActionTransitionCriteria.java:82)
     at org.springframework.webflow.engine.support.TransitionCriteriaChain.test(TransitionCriteriaChain.java:71)
     at org.springframework.webflow.engine.Transition.canExecute(Transition.java:195)
     at org.springframework.webflow.engine.Transition.execute(Transition.java:211)
     at org.springframework.webflow.engine.impl.FlowExecutionImpl.execute(FlowExecutionImpl.java:393)
     at org.springframework.webflow.engine.impl.RequestControlContextImpl.execute(RequestControlContextImpl.java:214)
     at org.springframework.webflow.engine.TransitionableState.handleEvent(TransitionableState.java:119)
     at org.springframework.webflow.engine.Flow.handleEvent(Flow.java:555)
     at org.springframework.webflow.engine.impl.FlowExecutionImpl.handleEvent(FlowExecutionImpl.java:388)
     at org.springframework.webflow.engine.impl.RequestControlContextImpl.handleEvent(RequestControlContextImpl.java:210)
     at org.springframework.webflow.engine.ViewState.handleEvent(ViewState.java:232)
     at org.springframework.webflow.engine.ViewState.resume(ViewState.java:196)
     at org.springframework.webflow.engine.Flow.resume(Flow.java:545)
     at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:261)
     at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
     at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
     at org.springframework.faces.webflow.JsfFlowHandlerAdapter.handle(JsfFlowHandlerAdapter.java:48)
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
     at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
     at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
     at      
  
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:   539)
     at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
     at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>these are the classes with hibernate mappings, in red are the relationships where the problem occurs</p>
  
  <pre><code> @Entity
     @Table(name = "CGN_CURRVIT")
     public class CurriculumVitaeEntityImpl extends AbstractJpaEntity implements
        CurriculumVitaeEntity {
  
     private static final long serialVersionUID = 1L;
  
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "ID")
     private Long id;
  
     @ManyToOne(targetEntity = PersonaEntityImpl.class, cascade = CascadeType.ALL)
     @JoinColumn(name = "PERSONA")
     private PersonaEntity persona;
  
  
     @OneToOne(mappedBy = "curriculumVitae", targetEntity = RecruitingEntityImpl.class)
     @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
     private List&lt;RecruitingEntity&gt; recruitingEntity;
     `
  
  
  
  
   `
     @Entity
     @Table(name = "CGN_RECR")
     public class RecruitingEntityImpl extends AbstractJpaEntity implements
        RecruitingEntity {
  
     /**
      * 
      */
     private static final long serialVersionUID = 1L;
     @Id
     @Column(name = "ID")
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
  
     @Column(name = "RECRUITING_ID")
     private Long recruitingId;
  
     @Column(name = "GIUDIZIO")
     private Long giudizio;       //valore da 1 a 5
  
     @Column(name = "NOTE")
     private String note;
  
     @OneToOne(targetEntity = ValutazioneEntityImpl.class, cascade = CascadeType.ALL, fetch =           
  
     FetchType.EAGER)
     @JoinColumn(name = "VALUTAZIONE")
     private ValutazioneEntity valutazione = new ValutazioneEntityImpl();
  
  
    @OneToOne(targetEntity = CurriculumVitaeEntityImpl.class, cascade = CascadeType.ALL )
     @JoinColumn(name = "CURRICULUM_ID")
     @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
     private CurriculumVitaeEntity curriculumVitae;
  
     .......
     }
  </code></pre>
  
  <p>This is the class which provides data storage on the database</p>
  
  <p>public class RecruitingServiceImpl extends AbstractDmsService implements
        RecruitingService {</p>
  
  <p>public void saveRecruiting(RecruitingEntity recruiting) {</p>
  
  <pre><code>  /* in curriculumVitae ci sono tutti i dati da salvare */
  
    /*
     * Cerco il persistenceStatus dal db e lo setto al curriculum vitae e
     * alla persona associata
     */
    PersistenceStatus persistenceStatus = this.entityManager.find(
          PersistenceStatusImpl.class, 1L);
    recruiting.setPersistenceStatus(persistenceStatus);
  
    ValutazioneTipologicalEntity valutazioneEntity = valutazioneService
          .findByDescription(recruiting.getValutazione().getValutazione()
                .getDescription());
    recruiting.getValutazione().setValutazione(valutazioneEntity);
  
    recruiting.getValutazione()
          .setPersistenceStatus(persistenceStatus);
  
    entityManager.persist(recruiting);
  
    }
  </code></pre>