#39 - Added example of SpEL usage in manually defined JPA queries.
This commit is contained in:
committed by
Oliver Gierke
parent
a579f6e879
commit
30386210c4
@@ -118,4 +118,13 @@ public interface SimpleUserRepository extends CrudRepository<User, Long> {
|
||||
* @return
|
||||
*/
|
||||
List<User> findTop2By(Sort sort);
|
||||
|
||||
/**
|
||||
* Return all the users with the given firstname or lastname. Makes use of SpEL (Spring Expression Language).
|
||||
*
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
@Query("select u from User u where u.firstname = :#{#user.firstname} or u.lastname = :#{#user.lastname}")
|
||||
Iterable<User> findByFirstnameOrLastname(@Param("user") User user);
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
*/
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.domain.Sort.Direction.*;
|
||||
|
||||
@@ -185,4 +184,27 @@ public class SimpleUserRepositoryTests {
|
||||
assertThat(resultDesc.size(), is(2));
|
||||
assertThat(resultDesc, hasItems(user1, user2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findByFirstnameOrLastnameUsingSpEL() {
|
||||
|
||||
User first = new User();
|
||||
first.setLastname("lastname");
|
||||
|
||||
User second = new User();
|
||||
second.setFirstname("firstname");
|
||||
|
||||
User third = new User();
|
||||
|
||||
repository.save(Arrays.asList(first, second, third));
|
||||
|
||||
User reference = new User();
|
||||
reference.setFirstname("firstname");
|
||||
reference.setLastname("lastname");
|
||||
|
||||
Iterable<User> users = repository.findByFirstnameOrLastname(reference);
|
||||
|
||||
assertThat(users, is(iterableWithSize(2)));
|
||||
assertThat(users, hasItems(first, second));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user