I have some objects I cannot delete, and must update a common field named 'deleted' instead of it. I read there that I can write generic querys, using #{#entityName}
. For that reason I tried to override CrudRepository#delete(…)
method like this:
public interface DeleteableRepository<T, ID extends Serializable> extends CrudRepository<T,ID>{
@Override
@Query("UPDATE #{#entityName} x set x.deleted = 1 where x.id = ?1")
public void delete(ID id);
}
But the I have a unit test that shows me wrong!
@Test
public void testDelete() {
SomeDeleteableObject sdo = new SomeDeletableObject();
sdo = getDeleteableRepository().create(sdo);
Assert.assertNotNull(sdo);
Assert.assertNotNull(sdo.getId());
Assert.assertFalse(sdo.isDeleted());
getDeleteableRepository().delete(sdo);
sdo = getDeleteableRepository().findOne(sdo.getId());
//Fails here
}
Isn't it possible to override CrudRepository
methods like that?
For modifying queries you need to add an @Modifying
to the method.
Be sure you are aware of the side effects of the approach you chose:
EntityManager
caches. Thus a subsequent findOne(…)
might/will still return the old instance of the object you tried to delete in case the EntityManager
had already loaded it. To prevent that, set the clearAutomatically
flag in @Modifying
to true
but be aware that this will cause all pending changes being wiped out. @PreUpdate
event will not get notified. Also any cascade operations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.