DATAJPA-117 - Added nativeQuery flag to @Query.
@Query can now be used to execute native queries by setting the nativeQuery flag of the annotation to true. Polished JavaDoc of the @Query annotation. Upgraded EclipseLink dependency to 2.3.1 as we stumble over a NullPointerException otherwise which is fixed in the current one.
This commit is contained in:
@@ -680,6 +680,20 @@ public class UserRepositoryTests {
|
||||
assertThat(result, hasItems(secondUser, thirdUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-117
|
||||
*/
|
||||
@Test
|
||||
public void executesNativeQueryCorrectly() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<User> result = repository.findNativeByLastname("Matthews");
|
||||
|
||||
assertThat(result, hasItem(thirdUser));
|
||||
assertThat(result.size(), is(1));
|
||||
}
|
||||
|
||||
private void flushTestUsers() {
|
||||
|
||||
firstUser = repository.save(firstUser);
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
@@ -54,7 +55,8 @@ public class JpaQueryMethodUnitTests {
|
||||
@Mock
|
||||
RepositoryMetadata metadata;
|
||||
|
||||
Method repositoryMethod, invalidReturnType, pageableAndSort, pageableTwice, sortableTwice, modifyingMethod;
|
||||
Method repositoryMethod, invalidReturnType, pageableAndSort, pageableTwice, sortableTwice, modifyingMethod,
|
||||
nativeQuery;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
@@ -70,6 +72,8 @@ public class JpaQueryMethodUnitTests {
|
||||
|
||||
sortableTwice = InvalidRepository.class.getMethod(METHOD_NAME, String.class, Sort.class, Sort.class);
|
||||
modifyingMethod = UserRepository.class.getMethod("renameAllUsersTo", String.class);
|
||||
|
||||
nativeQuery = InvalidRepository.class.getMethod("findByLastname", String.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,6 +84,8 @@ public class JpaQueryMethodUnitTests {
|
||||
|
||||
assertEquals("User.findByLastname", method.getNamedQueryName());
|
||||
assertThat(method.isCollectionQuery(), is(true));
|
||||
assertThat(method.getAnnotatedQuery(), is(nullValue()));
|
||||
assertThat(method.isNativeQuery(), is(false));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -188,6 +194,16 @@ public class JpaQueryMethodUnitTests {
|
||||
assertThat(queryMethod.getNamedQueryName(), is("SpecialUser.findSpecialUsersByLastname"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-117
|
||||
*/
|
||||
@Test
|
||||
public void discoversNativeQuery() {
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(nativeQuery, metadata, extractor);
|
||||
assertThat(method.isNativeQuery(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface to define invalid repository methods for testing.
|
||||
*
|
||||
@@ -218,5 +234,8 @@ public class JpaQueryMethodUnitTests {
|
||||
// Modifying and Sort is not allowed
|
||||
@Modifying
|
||||
void updateMethod(String firstname, Sort sort);
|
||||
|
||||
@Query(value = "query", nativeQuery = true)
|
||||
List<User> findByLastname(String lastname);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
@@ -33,6 +34,9 @@ import org.mockito.Mockito;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
@@ -99,4 +103,53 @@ public class SimpleJpaQueryUnitTests {
|
||||
verify(query, times(0)).setFirstResult(anyInt());
|
||||
verify(query, times(0)).setMaxResults(anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void discoversNativeQuery() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class);
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
SimpleJpaQuery jpaQuery = new SimpleJpaQuery(queryMethod, em);
|
||||
|
||||
Class<?> type = Mockito.any();
|
||||
when(em.createNativeQuery(Mockito.anyString(), type)).thenReturn(query);
|
||||
when(metadata.getReturnedDomainClass(method)).thenReturn((Class) User.class);
|
||||
|
||||
jpaQuery.createQuery(new Object[] { "Matthews" });
|
||||
|
||||
verify(em).createNativeQuery("SELECT u FROM User u WHERE u.lastname = ?1", User.class);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsNativeQueryWithDynamicSort() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Sort.class);
|
||||
rejectsNativeQuery(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsNativeQueryWithPageable() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Pageable.class);
|
||||
rejectsNativeQuery(method);
|
||||
}
|
||||
|
||||
private void rejectsNativeQuery(Method method) {
|
||||
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
new SimpleJpaQuery(queryMethod, em);
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
|
||||
@Query(value = "SELECT u FROM User u WHERE u.lastname = ?1", nativeQuery = true)
|
||||
List<User> findNativeByLastname(String lastname);
|
||||
|
||||
@Query(value = "SELECT u FROM User u WHERE u.lastname = ?1", nativeQuery = true)
|
||||
List<User> findNativeByLastname(String lastname, Sort sort);
|
||||
|
||||
@Query(value = "SELECT u FROM User u WHERE u.lastname = ?1", nativeQuery = true)
|
||||
List<User> findNativeByLastname(String lastname, Pageable pageable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,9 +189,12 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
|
||||
|
||||
List<User> findByAgeLessThanEqual(int age);
|
||||
|
||||
/**
|
||||
* @see DATADOC-86 - Count execution for group by
|
||||
*/
|
||||
@Query("select u.lastname from User u group by u.lastname")
|
||||
Page<String> findByLastnameGrouped(Pageable pageable);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-117
|
||||
*/
|
||||
@Query(value = "SELECT * FROM User WHERE lastname = ?1", nativeQuery = true)
|
||||
List<User> findNativeByLastname(String lastname);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user