From 5b2cef49381f87322e3bb016c702593dfd1a6c00 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 12 Mar 2012 19:18:12 +0100 Subject: [PATCH] DATAJPA-169 - Fixed regression in SimpleJpaQuery. When using a projection to a collection property in a manually defined query we must not set the domain class type when creating the query: interface UserRepository implements Repository { @Query("select u.colleagues from User u where u = ?1") List findColleguesFor(User user); } We now must not call em.createQuery(queryString, User.class) as we were before as this causes some persistence providers (tested with Hibernate) to reject the query type. Thus we now simply create an untyped query as we don't need the typing here. Unfortunately we cannot activate the test case currently as OpenJPA does not support using projections on a collection property and breaks the bootstrap process of the integration test. --- .../jpa/repository/query/SimpleJpaQuery.java | 2 +- .../jpa/repository/UserRepositoryTests.java | 17 +++++++++++++++++ .../jpa/repository/sample/UserRepository.java | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java index 5fbec53e4..20b1aacd1 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java @@ -93,7 +93,7 @@ final class SimpleJpaQuery extends AbstractJpaQuery { .createNativeQuery(sortedQueryString, method.getReturnedObjectType()); } else { query = method.isModifyingQuery() ? getEntityManager().createQuery(sortedQueryString) : getEntityManager() - .createQuery(sortedQueryString, method.getReturnedObjectType()); + .createQuery(sortedQueryString); } return createBinder(values).bindAndPrepare(query); diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index 711a8ce6e..83fa6ecb5 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -32,6 +32,7 @@ import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -735,6 +736,22 @@ public class UserRepositoryTests { assertThat(result, hasItem(firstUser)); } + /** + * Ignored until the query declaration is supported by OpenJPA. + */ + @Test + @Ignore + public void executesAnnotatedCollectionMethodCorrectly() { + + flushTestUsers(); + firstUser.addColleague(thirdUser); + repository.save(firstUser); + + List result = null; // repository.findColleaguesFor(firstUser); + assertThat(result.size(), is(1)); + assertThat(result, hasItem(thirdUser)); + } + protected void flushTestUsers() { firstUser = repository.save(firstUser); diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java index 4f310f930..20bff35e6 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java @@ -207,4 +207,10 @@ public interface UserRepository extends JpaRepository, JpaSpecifi * @see DATAJPA-132 */ List findByActiveFalse(); + + /** + * Commented out until OpenJPA supports this. + */ + // @Query("select u.colleagues from User u where u = ?1") + // List findColleaguesFor(User user); }