BAEL-2827 added one example for @query

This commit is contained in:
sumit.sg34 2019-03-22 23:20:12 +05:30
parent 117e4038b9
commit 2b8c73c376
2 changed files with 19 additions and 0 deletions

View File

@ -3,6 +3,9 @@ package com.baeldung.repository;
import java.util.List; import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.baeldung.entity.Fruit; import com.baeldung.entity.Fruit;
@ -17,4 +20,8 @@ public interface FruitRepository extends JpaRepository<Fruit, Long> {
Long removeByName(String name); Long removeByName(String name);
List<Fruit> removeByColor(String color); List<Fruit> removeByColor(String color);
@Modifying
@Query("delete from Fruit f where f.name=:name or f.color=:color")
List<Fruit> deleteFruits(@Param("name") String name, @Param("color") String color);
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.repository; package com.baeldung.repository;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List; import java.util.List;
@ -62,4 +63,15 @@ class FruitRepositoryIntegrationTest {
assertEquals("deleted fruit count is not matching", 1, deletedFruitCount.intValue()); assertEquals("deleted fruit count is not matching", 1, deletedFruitCount.intValue());
} }
@Transactional
@Test
@Sql(scripts = { "/test-fruit-data.sql" })
public void givenFruits_WhenDeletedByColorOrName_DeletedFruitShouldReturn() {
List<Fruit> fruits = fruitRepository.deleteFruits("apple", "green");
assertEquals("number of fruits are not matching", 3, fruits.size());
fruits.forEach(fruit -> assertTrue("Its not a green fruit or apple", ("green".equals(fruit.getColor())) || "apple".equals(fruit.getColor())));
}
} }