<p>I have this exception while changing parent for entity (@OneToMany relationship).</p>

<blockquote>
  <p>Entity parent update - org.hibernate.HibernateException: identifier of
  an instance of {Entity} was altered from 1 to 2</p>
</blockquote>

<p>This exception occurs and can be reproduce only for service running in Kubernetes after some time. I mean that it isn't reproduced from the very begging of container life and some number of updates completed successfully.   </p>

<p>The method that does the update on the entities looks like this: </p>

<pre><code>@Transactional
    @Override
    public Optional&lt;EntityT&gt; update(EntityT entity) {

        entity.setIsConfirmed(true);

        return getRepository().findById(entity.getId())
                .map(entityToUpdate -&gt; updateEntity(entity, entityToUpdate));
    }


private EntityT updateEntity(EntityT entity, EntityT entityToUpdate) {
        modelMapper.map(entity, entityToUpdate);

        getParentRepository().ifPresent(parentRepository -&gt;
                entity.getParent().ifPresent(parentEntity -&gt;
                        parentRepository.findById(parentEntity.getId()).ifPresent(entityToUpdate::setParent))
        );

        entityToUpdate.setVersionTs(getCurrentTime());
        return getRepository().save(entityToUpdate);
    }
</code></pre>

<p>Spring boot version - 2.1.2 Hibernate 5.3.7 also try 5.4.1 - the same result.</p>

<p>Also set spring jpa properties to</p>

<pre><code>spring:
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    generate-ddl: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        jdbc:
          batch_size: 100
        flushMode: "ALWAYS"
        order_inserts: true
        order_updates: true
</code></pre>

<p>Also tried different images for container open-jdk8 / oracle-jdk8</p>

<p>Could anybody advice some solution?</p>

<p>Thanks in advice.</p>

<p>The main issue was in mapper. Instead of parent replacing it change only id for fetched parent. Then we replace parent but fetched parent remains in cache (with new id) and Hibernate after some time try to flush this changes into DB.  </p>
