|
|
|
@ -15,6 +15,13 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.springframework.data.document.mongodb.repository; |
|
|
|
package org.springframework.data.document.mongodb.repository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.data.document.mongodb.query.Criteria.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import org.bson.types.ObjectId; |
|
|
|
import org.bson.types.ObjectId; |
|
|
|
import org.springframework.data.document.mongodb.MongoTemplate; |
|
|
|
import org.springframework.data.document.mongodb.MongoTemplate; |
|
|
|
import org.springframework.data.document.mongodb.query.Criteria; |
|
|
|
import org.springframework.data.document.mongodb.query.Criteria; |
|
|
|
@ -26,199 +33,189 @@ import org.springframework.data.domain.Sort; |
|
|
|
import org.springframework.data.repository.PagingAndSortingRepository; |
|
|
|
import org.springframework.data.repository.PagingAndSortingRepository; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.springframework.data.document.mongodb.query.Criteria.where; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Repository base implementation for Mongo. |
|
|
|
* Repository base implementation for Mongo. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Oliver Gierke |
|
|
|
* @author Oliver Gierke |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> { |
|
|
|
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> { |
|
|
|
|
|
|
|
|
|
|
|
private final MongoTemplate template; |
|
|
|
private final MongoTemplate template; |
|
|
|
private final MongoEntityInformation<T, ID> entityInformation; |
|
|
|
private final MongoEntityInformation<T, ID> entityInformation; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}. |
|
|
|
* Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param metadata |
|
|
|
* @param metadata |
|
|
|
* @param template |
|
|
|
* @param template |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoTemplate template) { |
|
|
|
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoTemplate template) { |
|
|
|
|
|
|
|
|
|
|
|
Assert.notNull(template); |
|
|
|
Assert.notNull(template); |
|
|
|
Assert.notNull(metadata); |
|
|
|
Assert.notNull(metadata); |
|
|
|
this.entityInformation = metadata; |
|
|
|
this.entityInformation = metadata; |
|
|
|
this.template = template; |
|
|
|
this.template = template; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
* (non-Javadoc) |
|
|
|
* (non-Javadoc) |
|
|
|
* |
|
|
|
* |
|
|
|
* @see org.springframework.data.repository.Repository#save(java.lang.Object) |
|
|
|
* @see org.springframework.data.repository.Repository#save(java.lang.Object) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public T save(T entity) { |
|
|
|
public T save(T entity) { |
|
|
|
|
|
|
|
|
|
|
|
template.save(entityInformation.getCollectionName(), entity); |
|
|
|
template.save(entityInformation.getCollectionName(), entity); |
|
|
|
return entity; |
|
|
|
return entity; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
* (non-Javadoc) |
|
|
|
* (non-Javadoc) |
|
|
|
* |
|
|
|
* |
|
|
|
* @see org.springframework.data.repository.Repository#save(java.lang.Iterable) |
|
|
|
* @see org.springframework.data.repository.Repository#save(java.lang.Iterable) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<T> save(Iterable<? extends T> entities) { |
|
|
|
public List<T> save(Iterable<? extends T> entities) { |
|
|
|
|
|
|
|
|
|
|
|
List<T> result = new ArrayList<T>(); |
|
|
|
List<T> result = new ArrayList<T>(); |
|
|
|
|
|
|
|
|
|
|
|
for (T entity : entities) { |
|
|
|
for (T entity : entities) { |
|
|
|
save(entity); |
|
|
|
save(entity); |
|
|
|
result.add(entity); |
|
|
|
result.add(entity); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
* (non-Javadoc) |
|
|
|
* (non-Javadoc) |
|
|
|
* |
|
|
|
* |
|
|
|
* @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) |
|
|
|
* @see org.springframework.data.repository.Repository#findById(java.io.Serializable ) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public T findById(ID id) { |
|
|
|
public T findById(ID id) { |
|
|
|
|
|
|
|
|
|
|
|
return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); |
|
|
|
return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Query getIdQuery(Object id) { |
|
|
|
private Query getIdQuery(Object id) { |
|
|
|
|
|
|
|
|
|
|
|
return new Query(getIdCriteria(id)); |
|
|
|
return new Query(getIdCriteria(id)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Criteria getIdCriteria(Object id) { |
|
|
|
private Criteria getIdCriteria(Object id) { |
|
|
|
ObjectId objectId = template.getConverter().convertObjectId(id); |
|
|
|
ObjectId objectId = template.getConverter().convertObjectId(id); |
|
|
|
return where(entityInformation.getIdAttribute()).is(objectId); |
|
|
|
return where(entityInformation.getIdAttribute()).is(objectId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public T findOne(ID id) { |
|
|
|
|
|
|
|
return null; |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#exists(java.io.Serializable ) |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public boolean exists(ID id) { |
|
|
|
* @see org.springframework.data.repository.Repository#exists(java.io.Serializable ) |
|
|
|
|
|
|
|
*/ |
|
|
|
return findById(id) != null; |
|
|
|
public boolean exists(ID id) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return findById(id) != null; |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#count() |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public Long count() { |
|
|
|
* @see org.springframework.data.repository.Repository#count() |
|
|
|
|
|
|
|
*/ |
|
|
|
return template.getCollection(entityInformation.getCollectionName()).count(); |
|
|
|
public Long count() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return template.getCollection(entityInformation.getCollectionName()).count(); |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#delete(java.lang.Object) |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public void delete(T entity) { |
|
|
|
* @see org.springframework.data.repository.Repository#delete(java.lang.Object) |
|
|
|
|
|
|
|
*/ |
|
|
|
template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity))); |
|
|
|
public void delete(T entity) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity))); |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#delete(java.lang.Iterable) |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public void delete(Iterable<? extends T> entities) { |
|
|
|
* @see org.springframework.data.repository.Repository#delete(java.lang.Iterable) |
|
|
|
|
|
|
|
*/ |
|
|
|
for (T entity : entities) { |
|
|
|
public void delete(Iterable<? extends T> entities) { |
|
|
|
delete(entity); |
|
|
|
|
|
|
|
} |
|
|
|
for (T entity : entities) { |
|
|
|
} |
|
|
|
delete(entity); |
|
|
|
|
|
|
|
} |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#deleteAll() |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public void deleteAll() { |
|
|
|
* @see org.springframework.data.repository.Repository#deleteAll() |
|
|
|
|
|
|
|
*/ |
|
|
|
template.dropCollection(entityInformation.getCollectionName()); |
|
|
|
public void deleteAll() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template.dropCollection(entityInformation.getCollectionName()); |
|
|
|
/* (non-Javadoc) |
|
|
|
} |
|
|
|
* @see org.springframework.data.repository.Repository#findAll() |
|
|
|
|
|
|
|
*/ |
|
|
|
/* (non-Javadoc) |
|
|
|
public List<T> findAll() { |
|
|
|
* @see org.springframework.data.repository.Repository#findAll() |
|
|
|
return findAll(new Query()); |
|
|
|
*/ |
|
|
|
} |
|
|
|
public List<T> findAll() { |
|
|
|
|
|
|
|
return findAll(new Query()); |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll |
|
|
|
* (non-Javadoc) |
|
|
|
* (org.springframework.data.domain.Pageable) |
|
|
|
* |
|
|
|
*/ |
|
|
|
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll |
|
|
|
public Page<T> findAll(final Pageable pageable) { |
|
|
|
* (org.springframework.data.domain.Pageable) |
|
|
|
|
|
|
|
*/ |
|
|
|
Long count = count(); |
|
|
|
public Page<T> findAll(final Pageable pageable) { |
|
|
|
List<T> list = findAll(QueryUtils.applyPagination(new Query(), pageable)); |
|
|
|
|
|
|
|
|
|
|
|
Long count = count(); |
|
|
|
return new PageImpl<T>(list, pageable, count); |
|
|
|
List<T> list = findAll(QueryUtils.applyPagination(new Query(), pageable)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new PageImpl<T>(list, pageable, count); |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll |
|
|
|
* (non-Javadoc) |
|
|
|
* (org.springframework.data.domain.Sort) |
|
|
|
* |
|
|
|
*/ |
|
|
|
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll |
|
|
|
public List<T> findAll(final Sort sort) { |
|
|
|
* (org.springframework.data.domain.Sort) |
|
|
|
|
|
|
|
*/ |
|
|
|
return findAll(QueryUtils.applySorting(new Query(), sort)); |
|
|
|
public List<T> findAll(final Sort sort) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return findAll(QueryUtils.applySorting(new Query(), sort)); |
|
|
|
/* |
|
|
|
} |
|
|
|
* (non-Javadoc) |
|
|
|
|
|
|
|
* |
|
|
|
/* |
|
|
|
* @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable) |
|
|
|
* (non-Javadoc) |
|
|
|
*/ |
|
|
|
* |
|
|
|
public List<T> findAll(Iterable<ID> ids) { |
|
|
|
* @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable) |
|
|
|
|
|
|
|
*/ |
|
|
|
Query query = null; |
|
|
|
public List<T> findAll(Iterable<ID> ids) { |
|
|
|
|
|
|
|
|
|
|
|
for (ID id : ids) { |
|
|
|
Query query = null; |
|
|
|
if (query == null) { |
|
|
|
|
|
|
|
query = getIdQuery(id); |
|
|
|
for (ID id : ids) { |
|
|
|
} else { |
|
|
|
if (query == null) { |
|
|
|
query = new Query().or(getIdQuery(id)); |
|
|
|
query = getIdQuery(id); |
|
|
|
} |
|
|
|
} else { |
|
|
|
} |
|
|
|
query = new Query().or(getIdQuery(id)); |
|
|
|
|
|
|
|
} |
|
|
|
return findAll(query); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return findAll(query); |
|
|
|
private List<T> findAll(Query query) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (query == null) { |
|
|
|
private List<T> findAll(Query query) { |
|
|
|
return Collections.emptyList(); |
|
|
|
|
|
|
|
} |
|
|
|
if (query == null) { |
|
|
|
|
|
|
|
return Collections.emptyList(); |
|
|
|
return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|