32

I am trying to create a custom deleteBy method query in my repository. It seems that instead of deleting, hibernate is making a select statement.

public interface ContactRepository extends JpaRepository<Contact, Integer> {

    Integer deleteByEmailAddress(String emailAddress);

    //and this one works
    Contact findContactByName(String name);
}

and here is what Hibernate is trying to do:

Hibernate: select contact0_.id as id1_2_, contact0_.emailAddress as >emailAdd2_2_, contact0_.name as name3_2_ from Contact contact0_ where >contact0_.emailAddress=?

What am I missing? Do I have to make a special configuration in order to make delete work?

dur
  • 15,689
  • 25
  • 79
  • 125
Alexandru Pirvu
  • 505
  • 1
  • 4
  • 12

3 Answers3

46

Is the delete not working or not working as how you'd expect? Typically an entity has to be managed before it can be deleted, so a JPA provider (hibernate in your case) will load (the query you see) the entity first, then issue the delete.

If you're only seeing the query, but no corresponding delete, then some possibilities are:

  1. there's nothing to delete, make sure the record is there in the db
  2. the delete needs to be part of a transaction. I believe the Spring data CRUD ops are transactional by default, if not just make sure whatever is calling deleteByEmailAddress is transactional

Note: you can avoid the select when removing an entity using a modifying query delete, example below:

// NOTE: you have return void
@Modifying
@Transactional
@Query(value="delete from Contact c where c.emailAddress = ?1")
void deleteByEmailAddress(String emailAddress)
ikumen
  • 11,275
  • 4
  • 41
  • 41
  • 6
    It works after i annotate the method with @transactional. Thank you a lot, sir! NOTE: It also worked with Query but i just wanted a functional method query. – Alexandru Pirvu Jul 11 '15 at 09:17
  • 2
    thanks about `// NOTE: you have return void` - I'll tried all but didn't think about this, it was the reason in my case! – yetanothercoder Mar 27 '16 at 22:09
  • @ikumen Query is working fine with Modifying and transactional annotation but in database row is still there – Abhij Sep 01 '17 at 15:56
  • just to add few more points to this list: in entities you might want to add entityManager.flush() and entityManager.clear(). Maybe one is not seing delete because context is not flushed yet. And deleteBy... is not generating 'as-expected' queries when composite key is used. – Martin Mucha Feb 07 '19 at 12:46
  • 1
    In my case, I had a transactional method calling a NON transactional method calling the deleteBy. I thought that transaction would have been propagated (thats the default behaviour for spring transactions) but in that case the deleteBy didn't work. when I also annotated the second method with @transactional, it worked. – ihebiheb May 08 '19 at 17:34
  • my problem fixed after adding @Transactional – Morteza Sep 30 '20 at 13:54
3

In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove and count operations is accessible.

Spring Data: "delete by" is supported?

Community
  • 1
  • 1
  • 3
    That's where I started, but it's not answering my question. – Alexandru Pirvu Jul 11 '15 at 09:20
  • 1
    spring first make a call to query the row, then it make a second call to delete the row using the primary key...you shoud see something like Hibernate: delete from conatact where id=? in case id is the primay key.. – Cristian J. Brito Apr 13 '18 at 01:31
-3

Try to move the delete Query call to an service class exclusive for it:

@FunctionalInterface
public interface DeleteService {

    public int deleteIfExists(Date fechaProceso);

}

and implement it in DeleteServiceImpl.java for example

T21
  • 19
  • 2