I need to detach an entity from persistence context, within a spring-boot application.
I have the following base repository:
interface EntityRepository extends CrudRepository<Entity, Long>
Obviously this is not offering any detach(..)
-operation.
I found an answer, which is actually not working for me:
SO Post.
I tried the same, but it seems that my entity is not detached(as if im changing any field, it gets still persisted)
Custom Repo:
interface MyCustomEntityRepository {
void detach(Entity ent)
}
Interface Impl:
class MyCustomEntityRepositoryImpl implements MyCustomEntityRepository{
@PersistenceContext
private EntityManager em;
public void detach(Entity ent) {
em.detach(ent);
}
}
But I cant extend EntityRepository
with MyCustomEntityReposity
, as this results in:
No property detach found for type Entity!
I managed to get it compiled without errors, by not extending EntityRepository. Also changin CrudRepository
to JpaRepository
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 @EntityListener
, by checking the currently stored entity in db, with the currently changed entity instance, which should be detached.
Does anyone see some errors or give me a clue, what Im doing wrong ?
Using: Spring-boot(1.4.0-release), Spring 4, JPA