7

When using Spring-data it is possible to extend the CrudRepository.

How does this Repositories .delete() method work "under the hood"?

Also, is this method Transactional? If this is the case, is there any need to use @Transactional annotations when using Spring-data.

e.g is @Transactional needed here? :

Extending CrudRepository:

public interface PersonRepository extends CrudRepository<Person, Integer> {

}

Using delete method in service class:

 @Transactional
 public void deletePerson(Person person) {

        personRepository.delete(person);
    }

Edit: How would @Transactional work here?

 @Transactional
     public void deletePersonAndTable(Person person, Table table) {

            personRepository.delete(person);

            tableRepository.delete(Table);

        }
java123999
  • 6,974
  • 36
  • 77
  • 121
  • Define "_extend the CrudRepository_". Your example code is doing exactly that; no? A transaction is required to do anything with the database, so there is no need to for `@Transactional` annotation here - it would be required to group more than one DAO operation into a single transaction. – Boris the Spider Apr 15 '16 at 14:31
  • Did you try to remove `@Transactional` and see if the transaction is committed? – Arnaud Denoyelle Apr 15 '16 at 14:32
  • @BoristheSpider am just showing how my Repository extends the CrudRepository? Can you please define what you mean by: "required to group more than one DAO operation into a single transaction" ? thanks – java123999 Apr 15 '16 at 14:33
  • 1
    If in your method delete there is more one database access it need to be transactional so it make sense to add anotation Transactional. So if there is cascading eg : when you delete a person more one table are involved that's need to be transactional using anotation Transactional – Mr_Thorynque Apr 15 '16 at 14:50
  • @Mr_Thorynque im not sure what you mean? Can you give an example in Answer? – java123999 Apr 15 '16 at 15:08
  • @java123999 if our delete operation implies more one table it need a transaction in your exemple you need a transaction because you delete 2 objects. – Mr_Thorynque Apr 15 '16 at 15:38

1 Answers1

8

You don't need to add @Transactional annotations yourself.

From https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/:

Additionally, we can get rid of the @Transactional annotation for the method as the CRUD methods of the Spring Data JPA repository implementation are already annotated with @Transactional.

But you should add one in your DOA though, if you want to perform some actions sequently that should only be executed all together or none at all (that's what transactions are for).

Simon
  • 857
  • 5
  • 14
  • 1
    Thanks, in reference to your answer: can you please see my edit, im not sure how @transactional works in this case? – java123999 Apr 15 '16 at 15:34
  • tansactional anotation open a transaction if there is no transaction in the contexte nothing if there is one. – Mr_Thorynque Apr 15 '16 at 15:39
  • @Mr_Thorynque that's not really true in the general case. For example if you are not used JTA and you have an open transaction on another transactional resource. – Boris the Spider Apr 16 '16 at 12:14
  • @java123999 your new code will create one single transaction spanning both delete operations, assuming that "Table is an entity (with a quite confusing name) and the upper case is a type. An `@Transactional` will only open a new transaction if it wasn't called from within another method annotated with `@Transactional`. – Simon Apr 20 '16 at 16:22