How to detach entity from persistence context, if using Spring Data Repositories (Spring 4)
<p>I need to detach an entity from persistence context, within a spring-boot application.</p>

<p>I have the following base repository:</p>

<pre><code>interface EntityRepository extends CrudRepository&lt;Entity, Long&gt; 
</code></pre>

<p>Obviously this is not offering any <code>detach(..)</code>-operation.
I found an answer, which is actually not working for me:
<a href="https://stackoverflow.com/questions/26795436/spring-jparepository-detach-and-attach-entity">SO Post</a>.</p>

<p>I tried the same, but it seems that my entity is not detached(as if im changing any field, it gets still persisted)</p>

<p>Custom Repo:</p>

<pre><code>interface MyCustomEntityRepository {
  void detach(Entity ent)
}
</code></pre>

<p>Interface Impl:</p>

<pre><code>class MyCustomEntityRepositoryImpl implements MyCustomEntityRepository{
     @PersistenceContext
     private EntityManager em;

     public void detach(Entity ent) {
         em.detach(ent);
     }
}
</code></pre>

<p>But I cant extend <code>EntityRepository</code> with <code>MyCustomEntityReposity</code>, as this results in: </p>

<p><code>No property detach found for type Entity!</code>
I managed to get it compiled without errors, by not extending EntityRepository. Also changin <code>CrudRepository</code> to <code>JpaRepository</code>
But still my entity is not getting detached, but in linked post, the QA says, that it is working for him/her.
The actual reason for detaching the object, is to be able to perform some validations within an <code>@EntityListener</code>, by checking the currently stored entity in db, with the currently changed entity instance, which should be detached.</p>

<p>Does anyone see some errors or give me a clue, what Im doing wrong ?</p>

<p>Using: Spring-boot(1.4.0-release), Spring 4, JPA</p>