DATAJPA-506 - Re-enabled query method invocation with wrapper type results.

The custom handling for Blobs applies to strict conversion as it uses the simple method return type to decide whether to trigger a conversion. It now rather uses the returned domain type which automatically unwraps wrapper types like Guava's Optional.

Applied some polishing to the ConversionService setup in JpaQueryExecution to make more obvious that it's only mean to execute very specific conversions.

Added Guava as test dependency and explicit dependency for the Mysema APT plugin as the test scope only would make it invisible for the plugin.
This commit is contained in:
Oliver Gierke
2014-07-07 14:50:27 +02:00
parent eb341782d1
commit 2db3b9b5df
4 changed files with 48 additions and 5 deletions

12
pom.xml
View File

@@ -217,6 +217,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -409,6 +416,11 @@
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.3.0.Final</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
</dependencies>
</plugin>
<plugin>

View File

@@ -24,7 +24,9 @@ import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
@@ -44,10 +46,14 @@ import org.springframework.util.Assert;
*/
public abstract class JpaQueryExecution {
private static final DefaultConversionService conversionService = new DefaultConversionService();
private static final ConversionService CONVERSION_SERVICE;
static {
ConfigurableConversionService conversionService = new GenericConversionService();
conversionService.addConverter(JpaResultConverters.BlobToByteArrayConverter.INSTANCE);
CONVERSION_SERVICE = conversionService;
}
/**
@@ -81,7 +87,8 @@ public abstract class JpaQueryExecution {
return result;
}
return conversionService.convert(result, requiredType);
return CONVERSION_SERVICE.canConvert(result.getClass(), requiredType) ? CONVERSION_SERVICE.convert(result,
requiredType) : result;
}
/**

View File

@@ -63,6 +63,8 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.base.Optional;
/**
* Base integration test class for {@code UserRepository}. Loads a basic (non-namespace) Spring configuration file as
* well as Hibernate configuration to execute tests.
@@ -1573,6 +1575,20 @@ public class UserRepositoryTests {
assertThat(secondPage.getContent(), hasItems(youngest3));
}
/**
* @see DATAJPA-506
*/
@Test
public void invokesQueryWithWrapperType() {
flushTestUsers();
Optional<User> result = repository.findOptionalByEmailAddress("gierke@synyx.de");
assertThat(result.isPresent(), is(true));
assertThat(result.get(), is(firstUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -39,6 +39,8 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.base.Optional;
/**
* Repository interface for {@code User}s.
*
@@ -445,10 +447,16 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
* @see DATAJPA-551
*/
Slice<User> findTop2UsersBy(Pageable page);
/**
* @see DATAJPA-506
*/
@Query(value = "select u.binaryData from User u where u.id = ?", nativeQuery = true)
@Query(value = "select u.binaryData from User u where u.id = ?1", nativeQuery = true)
byte[] findBinaryDataByIdNative(Integer id);
/**
* @see DATAJPA-506
*/
@Query("select u from User u where u.emailAddress = ?1")
Optional<User> findOptionalByEmailAddress(String emailAddress);
}