#5 - Add sample for derived deleteBy.

Original pull request: #14.
This commit is contained in:
Christoph Strobl
2014-09-02 20:11:38 +02:00
committed by Oliver Gierke
parent 6fb5259e37
commit caf5ba4edc
2 changed files with 29 additions and 1 deletions

View File

@@ -29,6 +29,7 @@ import com.google.common.base.Optional;
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
public interface SimpleUserRepository extends CrudRepository<User, Long> {
@@ -72,4 +73,12 @@ public interface SimpleUserRepository extends CrudRepository<User, Long> {
*/
@Query("select u from User u where u.firstname = :name or u.lastname = :name")
List<User> findByFirstnameOrLastname(@Param("name") String name);
/**
* Returns the total number of entries deleted as their lastnames match the given one.
*
* @param lastname
* @return
*/
Long removeByLastname(String lastname);
}

View File

@@ -18,6 +18,7 @@ package example.springdata.jpa.simple;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
@@ -29,10 +30,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Intergration test showing the basic usage of {@link SimpleUserRepository}.
* Integration test showing the basic usage of {@link SimpleUserRepository}.
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@@ -89,4 +91,21 @@ public class SimpleUserRepositoryTests {
assertThat(repository.findByUsername("foobar").isPresent(), is(true));
}
@Test
public void removeByLastname() {
// create a 2nd user with the same lastname as user
User user2 = new User();
user2.setLastname(user.getLastname());
// create a 3rd user as control group
User user3 = new User();
user3.setLastname("no-positive-match");
repository.save(Arrays.asList(user, user2, user3));
assertThat(repository.removeByLastname(user.getLastname()), is(2L));
assertThat(repository.exists(user3.getId()), is(true));
}
}