DATAMONGO-1266 - Fixed domain type lookup for methods returning primitves.
If a repository query method returned a primitive, that primitive was exposed as domain type which e.g. caused deleteBy…(…) methods to fail that returned a void. We now shortcut the MongoEntityMetadata lookup in MongoQueryMethod to use the repository's domain type if a primitive or wrapper is returned.
This commit is contained in:
@@ -34,6 +34,7 @@ import org.springframework.data.repository.query.QueryMethod;
|
|||||||
import org.springframework.data.util.ClassTypeInformation;
|
import org.springframework.data.util.ClassTypeInformation;
|
||||||
import org.springframework.data.util.TypeInformation;
|
import org.springframework.data.util.TypeInformation;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -122,13 +123,22 @@ public class MongoQueryMethod extends QueryMethod {
|
|||||||
Class<?> returnedObjectType = getReturnedObjectType();
|
Class<?> returnedObjectType = getReturnedObjectType();
|
||||||
Class<?> domainClass = getDomainClass();
|
Class<?> domainClass = getDomainClass();
|
||||||
|
|
||||||
MongoPersistentEntity<?> returnedEntity = mappingContext.getPersistentEntity(getReturnedObjectType());
|
if (ClassUtils.isPrimitiveOrWrapper(returnedObjectType)) {
|
||||||
MongoPersistentEntity<?> managedEntity = mappingContext.getPersistentEntity(domainClass);
|
|
||||||
returnedEntity = returnedEntity == null ? managedEntity : returnedEntity;
|
|
||||||
MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity
|
|
||||||
: managedEntity;
|
|
||||||
|
|
||||||
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(), collectionEntity);
|
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) domainClass,
|
||||||
|
mappingContext.getPersistentEntity(domainClass));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
MongoPersistentEntity<?> returnedEntity = mappingContext.getPersistentEntity(returnedObjectType);
|
||||||
|
MongoPersistentEntity<?> managedEntity = mappingContext.getPersistentEntity(domainClass);
|
||||||
|
returnedEntity = returnedEntity == null ? managedEntity : returnedEntity;
|
||||||
|
MongoPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity
|
||||||
|
: managedEntity;
|
||||||
|
|
||||||
|
this.metadata = new SimpleMongoEntityMetadata<Object>((Class<Object>) returnedEntity.getType(),
|
||||||
|
collectionEntity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.metadata;
|
return this.metadata;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2011-2014 the original author or authors.
|
* Copyright 2011-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -177,6 +177,17 @@ public class MongoQueryMethodUnitTests {
|
|||||||
assertThat(method.getQueryMetaAttributes().getSnapshot(), is(true));
|
assertThat(method.getQueryMetaAttributes().getSnapshot(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1266
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void fallsBackToRepositoryDomainTypeIfMethodDoesNotReturnADomainType() throws Exception {
|
||||||
|
|
||||||
|
MongoQueryMethod method = queryMethod("deleteByUserName", String.class);
|
||||||
|
|
||||||
|
assertThat(method.getEntityInformation().getJavaType(), is(typeCompatibleWith(User.class)));
|
||||||
|
}
|
||||||
|
|
||||||
private MongoQueryMethod queryMethod(String name, Class<?>... parameters) throws Exception {
|
private MongoQueryMethod queryMethod(String name, Class<?>... parameters) throws Exception {
|
||||||
Method method = PersonRepository.class.getMethod(name, parameters);
|
Method method = PersonRepository.class.getMethod(name, parameters);
|
||||||
return new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), context);
|
return new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class), context);
|
||||||
@@ -210,6 +221,10 @@ public class MongoQueryMethodUnitTests {
|
|||||||
@Meta(snapshot = true)
|
@Meta(snapshot = true)
|
||||||
List<User> metaWithSnapshotUsage();
|
List<User> metaWithSnapshotUsage();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-1266
|
||||||
|
*/
|
||||||
|
void deleteByUserName(String userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SampleRepository extends Repository<Contact, Long> {
|
interface SampleRepository extends Repository<Contact, Long> {
|
||||||
|
|||||||
Reference in New Issue
Block a user