DATAGRAPH-1184 - Ensure that Query-annotated methods can return Optional<?> values.
This commit is contained in:
@@ -23,9 +23,9 @@ import org.springframework.data.neo4j.examples.movies.repo.UserRepository;
|
||||
* Example POJO {@link QueryResult} to test mapping onto arbitrary objects, even for properties that are on the nodes
|
||||
* but not in the entity classes.
|
||||
*
|
||||
* @see UserRepository
|
||||
* @author Adam George
|
||||
* @author Luanne Misquitta
|
||||
* @author Michael J. Simons
|
||||
*/
|
||||
@QueryResult
|
||||
public class UserQueryResult {
|
||||
@@ -57,6 +57,14 @@ public class UserQueryResult {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 23;
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.neo4j.examples.movies.repo;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -157,6 +158,12 @@ public interface UserRepository extends PersonRepository<User, Long> {
|
||||
|
||||
List<User> findByEmailAddressesNotContaining(String email);
|
||||
|
||||
@Query("MATCH (user:User) WHERE user.name=$0 RETURN user")
|
||||
Optional<User> findOptionalUserWithCustomQuery(String userName);
|
||||
|
||||
@Query("MATCH (user:User) WHERE user.name=$0 RETURN user.name AS userName, 42 as `user.age`")
|
||||
Optional<UserQueryResult> findOptionalUserResultWithCustomQuery(String userName);
|
||||
|
||||
@Query("MATCH (user:User) WHERE user.name=:#{#searchUser.name} RETURN user")
|
||||
User findUserByNameUsingSpElWithObject(@Param("searchUser") User user);
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -143,6 +144,49 @@ public class QueryIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATAGRAPH-1184
|
||||
public void customQueriesShouldBeAbleToReturnOptionals() {
|
||||
executeUpdate("CREATE (m:User {name:'Michael'})");
|
||||
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
public void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
Optional<User> user = userRepository.findOptionalUserWithCustomQuery("Michael");
|
||||
assertThat(user).map(User::getName).hasValue("Michael");
|
||||
}
|
||||
});
|
||||
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
public void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
Optional<User> user = userRepository.findOptionalUserWithCustomQuery("Joe User");
|
||||
assertThat(user).isNotPresent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test // DATAGRAPH-1184
|
||||
public void customQueriesShouldBeAbleToReturnOptionalQueryResults() {
|
||||
executeUpdate("CREATE (m:User {name:'Michael'})");
|
||||
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
public void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
Optional<UserQueryResult> user = userRepository.findOptionalUserResultWithCustomQuery("Michael");
|
||||
assertThat(user).map(UserQueryResult::getUserName).hasValue("Michael");
|
||||
assertThat(user).map(UserQueryResult::getAge).hasValue(42);
|
||||
}
|
||||
});
|
||||
|
||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
public void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
Optional<UserQueryResult> user = userRepository.findOptionalUserResultWithCustomQuery("Joe User");
|
||||
assertThat(user).isNotPresent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindUserByNameUsingSpElWithIndex() {
|
||||
executeUpdate("CREATE (m:User {name:'Michal'})");
|
||||
|
||||
Reference in New Issue
Block a user