# CrudRepository bulk delete



## Schuriko (9. Jan 2019)

Ich habe eine Repository 

```
public interface LanguageRepository extends CrudRepository<Language, Long> {
    @Modifying
    @Query("delete from Language l where l.id in ?1")
    void deleteWithIds(List<Long> ids);
}
```

und eine Test Funktion

```
@RunWith(SpringRunner.class)
@SpringBootTest
public class LanguageRepositoryTest {
    /**
     * test deleteWithIds
     */
    @Test
    public void testDeleteWidthIds() {
        // initialize
        String nameValue1="name";
        String isoValue1="iso";

        Language language1 = new Language(nameValue1, isoValue1);
       
        languageRepository.save(language1);

        String nameValue2="name2";
        String isoValue2="iso2";

        Language language2 = new Language(nameValue2, isoValue2);
       
        languageRepository.save(language2);
       
        String nameValue3="name3";
        String isoValue3="iso3";

        Language language3 = new Language(nameValue3, isoValue3);
       
        languageRepository.save(language3);
       
        List<Long> ids = new ArrayList<>();
       
        ids.add(language1.getId());
        ids.add(language3.getId());
       
        // test
        Optional<Language> foundLanguage1 = languageRepository.findById(language1.getId());
        Optional<Language> foundLanguage2 = languageRepository.findById(language2.getId());
        Optional<Language> foundLanguage3 = languageRepository.findById(language3.getId());
       
        assertTrue(foundLanguage1.isPresent());
        assertTrue(foundLanguage2.isPresent());
        assertTrue(foundLanguage3.isPresent());

        languageRepository.deleteWithIds(ids);

        Optional<Language> foundLanguage1 = languageRepository.findById(language1.getId());
        Optional<Language> foundLanguage2 = languageRepository.findById(language2.getId());
        Optional<Language> foundLanguage3 = languageRepository.findById(language3.getId());
       
        assertFalse(foundLanguage1.isPresent());
        assertTrue(foundLanguage2.isPresent());
        assertFalse(foundLanguage3.isPresent());
     }
}
```
Allerdings tritt bei 

```
languageRepository.deleteWithIds(ids);
```

ein Fehler auf


> org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query
> at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:396)
> at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
> at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
> ...



Ich verstehe nur nicht so ganz was ich hier falsch mache.


----------



## mrBrown (9. Jan 2019)

Schuriko hat gesagt.:


> Ich verstehe nur nicht so ganz was ich hier falsch mache.


`TransactionRequiredException` ist doch eigentlich ein sehr verständlicher Name


----------



## Schuriko (9. Jan 2019)

Hat sich soeben erledigt.
Für alle die es interessiert. Ich hatte vergessen @Transactional anzugeben. Korrekt muss es also lauten

```
@Test
    @Transactional
    public void testDeleteWidthIds() {
....
```


----------

