BAEL-2827 added one example for @query
This commit is contained in:
parent
117e4038b9
commit
2b8c73c376
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue