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<User, Long> {

  @Query("select u.colleagues from User u where u = ?1")
  List<User> 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.
This commit is contained in:
Oliver Gierke
2012-03-12 19:18:12 +01:00
parent e9632c7af4
commit 5b2cef4938
3 changed files with 24 additions and 1 deletions

View File

@@ -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);

View File

@@ -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<User> result = null; // repository.findColleaguesFor(firstUser);
assertThat(result.size(), is(1));
assertThat(result, hasItem(thirdUser));
}
protected void flushTestUsers() {
firstUser = repository.save(firstUser);

View File

@@ -207,4 +207,10 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
* @see DATAJPA-132
*/
List<User> findByActiveFalse();
/**
* Commented out until OpenJPA supports this.
*/
// @Query("select u.colleagues from User u where u = ?1")
// List<User> findColleaguesFor(User user);
}