title: Why am I obtaining this "detached entity passed to persist" when I first retrieve the object that has to be used in a @ManyToMany relationship?
body: <p>I am working on a Spring Boot project using Spring Data JPA. I am pretty new in Hibernate mapping and I have the following doubts.</p>
  <p><strong>NOTE:</strong> I first implemented my DB tables, then I am mapping these tables into Java entity classes.</p>
  <p>I have this User class:</p>
  <pre><code>@Entity
  @Table(name = &quot;portal_user&quot;)
  @Getter
  @Setter
  public class User implements Serializable {
       
      private static final long serialVersionUID = 5062673109048808267L;
      
      @Id
      @Column(name = &quot;id&quot;)
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
      
      @Column(name = &quot;first_name&quot;)
      private String firstName;
      
      @Column(name = &quot;middle_name&quot;)
      private String middleName;
      
      @Column(name = &quot;surname&quot;)
      private String surname;
      
      @Column(name = &quot;sex&quot;)
      private char sex;
      
      @Column(name = &quot;birthdate&quot;)
      private Date birthdate;
      
      @Column(name = &quot;tex_code&quot;)
      private String taxCode;
      
      @Column(name = &quot;e_mail&quot;)
      private String eMail;
      
      @Column(name = &quot;contact_number&quot;)
      private String contactNumber;
      
      @Temporal(TemporalType.DATE)
      @Column(name = &quot;created_at&quot;)
      private Date createdAt;
      
      @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = &quot;user&quot;, orphanRemoval = true)
      @JsonManagedReference
      private Set&lt;Address&gt; addressesList = new HashSet&lt;&gt;();
      
      //@OneToMany(mappedBy = &quot;user&quot;, cascade = CascadeType.ALL)
      //@JsonManagedReference
      //private Set&lt;User_UserType&gt; userToUserTypeAssociation = new HashSet&lt;&gt;();
      
      @ManyToMany(cascade = { CascadeType.ALL })
      @JoinTable(
          name = &quot;portal_user_user_type&quot;, 
          joinColumns = { @JoinColumn(name = &quot;portal_user_id_fk&quot;) }, 
          inverseJoinColumns = { @JoinColumn(name = &quot;user_type_id_fk&quot;) }
      )
      //Set&lt;UserType&gt; userTypes;
      List&lt;UserType&gt; userTypes;
  
  
      public User(String firstName, String middleName, String surname, char sex, Date birthdate, String taxCode,
              String eMail, String contactNumber, Date createdAt) {
          super();
          this.firstName = firstName;
          this.middleName = middleName;
          this.surname = surname;
          this.sex = sex;
          this.birthdate = birthdate;
          this.taxCode = taxCode;
          this.eMail = eMail;
          this.contactNumber = contactNumber;
          this.createdAt = createdAt;
      }
          
  
  }
  </code></pre>
  <p>as you can see it contains this **@ManyToMany relationshipt based on the <strong>portal_user_user_type</strong> association table:</p>
  <pre><code>@ManyToMany(cascade = { CascadeType.ALL })
  @JoinTable(
      name = &quot;portal_user_user_type&quot;, 
      joinColumns = { @JoinColumn(name = &quot;portal_user_id_fk&quot;) }, 
      inverseJoinColumns = { @JoinColumn(name = &quot;user_type_id_fk&quot;) }
  )
  //Set&lt;UserType&gt; userTypes;
  List&lt;UserType&gt; userTypes;
  </code></pre>
  <p>This is my <strong>UserType</strong> entity classes mapping the <strong>user_type</strong> DB table:</p>
  <pre><code>@Entity
  @Table(name = &quot;user_type&quot;)
  @Getter
  @Setter
  public class UserType implements Serializable {
      
      private static final long serialVersionUID = 6904959949570501298L;
  
      @Id
      @Column(name = &quot;id&quot;)
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
      
      @Column(name = &quot;type_name&quot;)
      private String typeName;
      
      @Column(name = &quot;description&quot;)
      private String description;
      
      @OneToMany(mappedBy = &quot;userType&quot;)
      @JsonManagedReference
      private Set&lt;User_UserType&gt; userToUserTypeAssociation = new HashSet&lt;&gt;();
      
  
      public UserType() {
          super();
          // TODO Auto-generated constructor stub
      }
      
      public UserType(String typeName, String description) {
          super();
          this.typeName = typeName;
          this.description = description;
      }
  
  }
  </code></pre>
  <p>Then I created this JUnit test method:</p>
  <pre><code>@SpringBootTest()
  @ContextConfiguration(classes = GetUserWsApplication.class)
  @TestMethodOrder(OrderAnnotation.class)
  public class UserRepositoryTest {
      
      @Autowired
      private UsersRepository userRepository;
      
      @Autowired 
      UserTypeRepository userTypeRepository;
      
      @Test
      @Order(1)
      public void testInsertUser() {
          
          User user = new User(&quot;Luca&quot;, null, &quot;Verdi&quot;, 'M', new Date(), &quot;XXX&quot;, &quot;xxx@gmail.com&quot;, &quot;329123456&quot;, new Date());
          
          Set&lt;Address&gt; addressesList = new HashSet&lt;&gt;();
          addressesList.add(new Address(&quot;Italy&quot;, &quot;RM&quot;, &quot;00100&quot;, &quot;Via XXX 123&quot;, &quot;near YYY&quot;, user));
          
          user.setAddressesList(addressesList);
          
          //List&lt;UserType&gt; userTypesList = userTypeRepository.findAll();
          List&lt;UserType&gt; userTypesList = new ArrayList&lt;UserType&gt;();
  
          //Set&lt;UserType&gt; userTypesList = new HashSet&lt;&gt;();
          UserType userType1 = new UserType(&quot;ADMIN&quot;, &quot;Admin user type !!!&quot;);
          UserType userType2 = new UserType(&quot;USER&quot;, &quot;Just a simple user...&quot;);
          
          userTypesList.add(userType1);
          userTypesList.add(userType2);
          
          //user.setUserToUserTypeAssociation(user_UserType_List);
          user.setUserTypes(userTypesList);
          
          userRepository.save(user);
          assertTrue(true);
          
      }
      
  }
  </code></pre>
  <p>Running this test method it works as I expected. Basically it inserted a new record into the <strong>portal_user</strong> table (mapped by my <strong>User</strong> entity class), then it inserted a record into the <strong>user_type</strong> table (mapped by the <strong>UserType</strong> entity class) and also it inserted the relationship into my <strong>Many To many</strong> association table named <strong>portal_user_user_type</strong>).</p>
  <p>Ok, now I have the following situation. The <strong>user_type</strong> table mapped by the <strong>UserType</strong> class is a typological table. It contains the predefined information related the possible user types (such as: &quot;ADMIN&quot;, &quot;MODERATOR&quot;, &quot;USER&quot;, etcetc).</p>
  <p>So this table must be pre populated and I have not to insert new records into this table but I have to use the existing record to populate my <strong>portal_user_user_type</strong> association table when a new user is created.</p>
  <p>So my idea was the following one:</p>
  <ol>
  <li>I first retrieve the list of possible UserType instances via a JPA repository.</li>
  <li>Differently from the previous code, instead to create these <strong>UserType</strong> instances I set this list into my user object.</li>
  </ol>
  <p>I don't know if this is the correct strategy. Anyway I have changed the previous code of my unit test method in this way and I am obtaining some problems:</p>
  <pre><code>@SpringBootTest()
  @ContextConfiguration(classes = GetUserWsApplication.class)
  @TestMethodOrder(OrderAnnotation.class)
  public class UserRepositoryTest {
      
      @Autowired
      private UsersRepository userRepository;
      
      @Autowired 
      UserTypeRepository userTypeRepository;
      
      
      @Test
      @Order(1)
      public void testInsertUser() {
          
          User user = new User(&quot;Luca&quot;, null, &quot;Verdi&quot;, 'M', new Date(), &quot;XXX&quot;, &quot;xxx@gmail.com&quot;, &quot;329123456&quot;, new Date());
          
          Set&lt;Address&gt; addressesList = new HashSet&lt;&gt;();
          addressesList.add(new Address(&quot;Italy&quot;, &quot;RM&quot;, &quot;00100&quot;, &quot;Via XXX 123&quot;, &quot;near YYY&quot;, user));
          
          user.setAddressesList(addressesList);
          
          List&lt;UserType&gt; userTypesList = userTypeRepository.findAll();
          
          user.setUserTypes(userTypesList);
          
          userRepository.save(user);
          assertTrue(true);
          
      }
      
  }
  </code></pre>
  <p>As you can see I am first retrieving the user types list by this JPA repository, then I am setting this list (instead the one that I manually created in the previous version of the test method):</p>
  <pre><code>List&lt;UserType&gt; userTypesList = userTypeRepository.findAll();
  user.setUserTypes(userTypesList);
  </code></pre>
  <p>The  retrieved user type is something like this (this list was retrieved by my <strong>userTypeRepository.findAll()</strong> call):</p>
  <p><a href="https://i.stack.imgur.com/qLpuo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qLpuo.png" alt="enter image description here" /></a></p>
  <p>So I set it using the <strong>setUserTypes()</strong> method (as I have done before with the UserType object that was manually created).</p>
  <p>The problem is that when this line is executed:</p>
  <pre><code>userRepository.save(user);
  </code></pre>
  <p>I obtain the following exception:</p>
  <pre><code>Hibernate: 
      insert 
      into
          portal_user
          (birthdate, contact_number, created_at, e_mail, first_name, middle_name, sex, surname, tex_code) 
      values
          (?, ?, ?, ?, ?, ?, ?, ?, ?)
  Hibernate: 
      insert 
      into
          address
          (country, notes, province, street, fk_user_id, zip_code) 
      values
          (?, ?, ?, ?, ?, ?)
  
  org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.easydefi.users.entity.UserType; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.easydefi.users.entity.UserType
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:297)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
      at jdk.proxy2/jdk.proxy2.$Proxy105.save(Unknown Source)
      at com.easydefi.users.tests.RepositoryTests.UserRepositoryTest.testInsertUser(UserRepositoryTest.java:77)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:567)
      at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
      at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
      at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
      at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
      at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
      at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
      at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
      at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:84)
      at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
  Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.easydefi.users.entity.UserType
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:120)
      at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:110)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:744)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:712)
      at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:298)
      at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:499)
      at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:423)
      at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:220)
      at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:532)
      at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:463)
      at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:426)
      at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:220)
      at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:153)
      at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:459)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:293)
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123)
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:128)
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:55)
      at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:99)
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:720)
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:706)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:567)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
      at jdk.proxy2/jdk.proxy2.$Proxy102.persist(Unknown Source)
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:597)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:567)
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
      at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529)
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:599)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163)
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
      ... 75 more
  </code></pre>
  <p>The problems seems to be this <strong>detached entity passed to persist</strong> but what it means?</p>
  <p>How can I try to fix this issue? and what could be a good approach to first retrieve the list of the possible <strong>UserTypes</strong> and then to insert a new User with the relation with these retrieved user types avoiding to inser them again in the DB?</p>