title: Getting "detached entity passed to persist" while trying to update entity
body: <p>I'm trying to update an object. As part of the process I load the old one from the database and "transfer" its property values to a new object.</p>
  
  <p>But when I try to persist the new object I get the following error:</p>
  
  <p><code>org.hibernate.PersistentObjectException: detached entity passed to persist: some.package.domain.Exercise</code></p>
  
  <p>My entity looks as the following</p>
  
  <pre><code>@Entity
  class Exercise(
          val name: String,
          val description: String,
          @JsonIgnore
          @ManyToOne
          val creator: User,
          @OneToMany(fetch = EAGER)
          @Fetch(SUBSELECT)
          val videos: MutableList&lt;Video&gt; = mutableListOf(),
          @Id
          @GeneratedValue(strategy = SEQUENCE)
          val id: Long = 0
  )
  </code></pre>
  
  <p>My code for persisting looks as the following</p>
  
  <pre><code>@Singleton
  @Transactional
  class ExerciseServiceDefault(override val repository: ExerciseRepository,
                               private val entityManager: EntityManager) : ExerciseService {
  
      override fun update(id: Long, exercise: Exercise): Exercise {
          val existing = get(id)
          val new = Exercise(exercise.name, exercise.description, existing.creator, existing.videos, existing.pictures, id)
          return repository.save(new)
  //        return entityManager.merge(new)
      }
  ...
  </code></pre>
  
  <p>If I change the above code to use <code>entityManager.merge(new)</code> everything works just fine. But I'd rather not have to inject the <code>entityManager</code>.</p>
  
  <p>Any clue about how I can make the updating happen using my <code>repository</code>?</p>
  
  <p>I've tried adding <code>cascade = [MERGE]</code> to my relations to no avail.</p>