DATAJPA-19 - Adapted generics and metadata changes made in core.
This commit is contained in:
@@ -100,7 +100,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
|
||||
protected List<?> readCollection(Query query) {
|
||||
|
||||
MongoEntityInformation<?> metadata = method.getEntityMetadata();
|
||||
MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
|
||||
|
||||
String collectionName = metadata.getCollectionName();
|
||||
return template.find(collectionName, query, metadata.getJavaType());
|
||||
@@ -155,7 +155,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
Object execute(Query query) {
|
||||
|
||||
MongoEntityInformation<?> metadata = method.getEntityMetadata();
|
||||
MongoEntityInformation<?, ?> metadata = method.getEntityInformation();
|
||||
int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count();
|
||||
|
||||
List<?> result = template.find(metadata.getCollectionName(), applyPagination(query, pageable),
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.document.mongodb.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -30,7 +31,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class MongoEntityInformation<T extends Object> extends AbstractEntityInformation<T> {
|
||||
class MongoEntityInformation<T extends Object, ID extends Serializable> extends AbstractEntityInformation<T, ID> {
|
||||
|
||||
private static final List<String> FIELD_NAMES = Arrays.asList("ID", "id", "_id");
|
||||
private Field field;
|
||||
@@ -64,11 +65,22 @@ class MongoEntityInformation<T extends Object> extends AbstractEntityInformation
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the collection the entity shall be persisted to.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getCollectionName() {
|
||||
|
||||
return StringUtils.uncapitalize(getJavaType().getSimpleName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the attribute that the id will be persisted to.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getIdAttribute() {
|
||||
|
||||
return "_id";
|
||||
@@ -82,8 +94,22 @@ class MongoEntityInformation<T extends Object> extends AbstractEntityInformation
|
||||
* org.springframework.data.repository.support.IdAware#getId(java.lang.Object
|
||||
* )
|
||||
*/
|
||||
public Object getId(Object entity) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public ID getId(Object entity) {
|
||||
|
||||
return ReflectionUtils.getField(field, entity);
|
||||
return (ID) ReflectionUtils.getField(field, entity);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.EntityInformation#getIdType()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Class<ID> getIdType() {
|
||||
|
||||
return (Class<ID>) field.getType();
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ import org.springframework.util.StringUtils;
|
||||
class MongoQueryMethod extends QueryMethod {
|
||||
|
||||
private final Method method;
|
||||
private final MongoEntityInformation<?> entityInformation;
|
||||
private final MongoEntityInformation<?, ?> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
|
||||
@@ -82,7 +82,7 @@ class MongoQueryMethod extends QueryMethod {
|
||||
* @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata()
|
||||
*/
|
||||
@Override
|
||||
public MongoEntityInformation<?> getEntityMetadata() {
|
||||
public MongoEntityInformation<?, ?> getEntityInformation() {
|
||||
|
||||
return entityInformation;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<MongoRepository<?, ?>> {
|
||||
public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID extends Serializable> extends RepositoryFactoryBeanSupport<T, S, ID> {
|
||||
|
||||
private MongoTemplate template;
|
||||
|
||||
@@ -101,14 +101,7 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
MongoEntityInformation<Object> info = new MongoEntityInformation<Object>(
|
||||
(Class<Object>) metadata.getDomainClass());
|
||||
return new SimpleMongoRepository<Object, Serializable>(info, template);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -162,6 +155,31 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
|
||||
super.validate(metadata, customImplementation);
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(
|
||||
Class<T> domainClass) {
|
||||
|
||||
return new MongoEntityInformation<T, ID>(domainClass);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.support.RepositoryMetadata)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
MongoEntityInformation<?, ?> info = getEntityInformation(
|
||||
metadata.getDomainClass());
|
||||
return new SimpleMongoRepository(info, template);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,7 +217,7 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
index.on(property, order);
|
||||
}
|
||||
|
||||
MongoEntityInformation<?> metadata = query.getQueryMethod().getEntityMetadata();
|
||||
MongoEntityInformation<?, ?> metadata = query.getQueryMethod().getEntityInformation();
|
||||
operations.ensureIndex(metadata.getCollectionName(), index);
|
||||
LOG.debug(String.format("Created index %s!", index.toString()));
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
|
||||
public PartTreeMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
|
||||
super(method, template);
|
||||
this.tree = new PartTree(method.getName(), method.getEntityMetadata().getJavaType());
|
||||
this.tree = new PartTree(method.getName(), method.getEntityInformation().getJavaType());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,7 +41,7 @@ import org.springframework.util.Assert;
|
||||
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> {
|
||||
|
||||
private final MongoTemplate template;
|
||||
private final MongoEntityInformation<T> entityInformation;
|
||||
private final MongoEntityInformation<T, ID> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}.
|
||||
@@ -49,7 +49,7 @@ public class SimpleMongoRepository<T, ID extends Serializable> implements Paging
|
||||
* @param metadata
|
||||
* @param template
|
||||
*/
|
||||
public SimpleMongoRepository(MongoEntityInformation<T> metadata, MongoTemplate template) {
|
||||
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(metadata);
|
||||
|
||||
@@ -31,8 +31,8 @@ public class MongoEntityMetadataUnitTests {
|
||||
@Test
|
||||
public void findsIdField() throws Exception {
|
||||
|
||||
MongoEntityInformation<Person> isNewAware =
|
||||
new MongoEntityInformation<Person>(Person.class);
|
||||
MongoEntityInformation<Person, Long> isNewAware =
|
||||
new MongoEntityInformation<Person, Long>(Person.class);
|
||||
|
||||
Person person = new Person();
|
||||
assertThat(isNewAware.isNew(person), is(true));
|
||||
@@ -44,7 +44,7 @@ public class MongoEntityMetadataUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsClassIfNoIdField() throws Exception {
|
||||
|
||||
new MongoEntityInformation<InvalidPerson>(InvalidPerson.class);
|
||||
new MongoEntityInformation<InvalidPerson, Long>(InvalidPerson.class);
|
||||
}
|
||||
|
||||
class Person {
|
||||
|
||||
@@ -7,6 +7,7 @@ Import-Package:
|
||||
Import-Template:
|
||||
org.springframework.beans.*;version="[3.0.0, 4.0.0)",
|
||||
org.springframework.core.*;version="[3.0.0, 4.0.0)",
|
||||
org.springframework.context.*;version="[3.0.0, 4.0.0)",
|
||||
org.springframework.dao.*;version="[3.0.0, 4.0.0)",
|
||||
org.springframework.util.*;version="[3.0.0, 4.0.0)",
|
||||
org.springframework.jmx.export.*;version="[3.0.0, 4.0.0)",
|
||||
|
||||
Reference in New Issue
Block a user