Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a delete method on a Spring Data CrudRepository?

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?

like image 445
Rafael T Avatar asked Apr 19 '14 17:04

Rafael T


People also ask

What is the use of crudrepository in spring?

CrudRepository is a Spring data interface and to use it we need to create our interface by extending CrudRepository for a specific type. Spring provides CrudRepository implementation class automatically at runtime. It contains methods such as save, findById, delete, count etc. If we want to add extra methods, we need to declare it in our interface.

How to delete an entity from the database in spring data?

As the name depicts, the delete () method allows us to delete an entity from the database table. It belongs to the CrudRepository interface defined by Spring Data. Let's first create a Product entity that we are going to use to save and delete to/from the database: Let's create ProductRepository which extends the CrudRepository interface.

How to delete a book from crudrepository?

Delete from Repository Among others, CrudRepository contains two methods: deleteById and deleteAll. Let's test these methods directly from our BookRepository:

How to test the deletebyid() method in Spring Boot?

In order to test the deleteById () method, we gonna use CommandLineRunner.run () method to execute the testing code while Spring boot application startup: After finishing the Spring boot application, you can able to see Hibernate-generated SQL statements in a console.


1 Answers

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:

  • Executing a manipulating query is pretty much bypassing all 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.
  • For query based data manipulation no lifecycle callbacks will be triggered and no cascades will be triggered on the level of the persistence context. This means, entity listeners listening to an @PreUpdate event will not get notified. Also any cascade operations
like image 75
Oliver Drotbohm Avatar answered Nov 04 '22 20:11

Oliver Drotbohm