DATADOC-78: Added code to MongoTemplate to check for overriden collection name when saving an entity without specifying a collection. Removed SLF4J from source code and reverted to commons-logging API for internal logging.

This commit is contained in:
Jon Brisbin
2011-04-04 15:14:42 -05:00
committed by J. Brisbin
parent b25e0c432e
commit 3fdd43bef8
16 changed files with 124 additions and 199 deletions

View File

@@ -82,45 +82,6 @@
<version>1.6.1</version> <version>1.6.1</version>
</dependency> </dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency> <dependency>
<groupId>javax.annotation</groupId> <groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId> <artifactId>jsr250-api</artifactId>

View File

@@ -26,45 +26,6 @@
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
</dependency> </dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency> <dependency>
<groupId>javax.annotation</groupId> <groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId> <artifactId>jsr250-api</artifactId>
@@ -95,34 +56,16 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -103,12 +103,6 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version> <version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
@@ -169,6 +163,7 @@
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version> <version>${org.slf4j.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>

View File

@@ -2,7 +2,6 @@ package org.springframework.data.document.persistence.test;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.data.persistence.document.mongo.MongoDocumentBacking;
//@DocumentEntity //@DocumentEntity
public class Resume { public class Resume {

View File

@@ -62,45 +62,6 @@
</exclusions> </exclusions>
</dependency> </dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<dependency> <dependency>
<groupId>com.mysema.querydsl</groupId> <groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId> <artifactId>querydsl-mongodb</artifactId>

View File

@@ -57,6 +57,8 @@ import org.springframework.data.document.mongodb.convert.MappingMongoConverter;
import org.springframework.data.document.mongodb.convert.MongoConverter; import org.springframework.data.document.mongodb.convert.MongoConverter;
import org.springframework.data.document.mongodb.convert.SimpleMongoConverter; import org.springframework.data.document.mongodb.convert.SimpleMongoConverter;
import org.springframework.data.document.mongodb.index.IndexDefinition; import org.springframework.data.document.mongodb.index.IndexDefinition;
import org.springframework.data.document.mongodb.mapping.Document;
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity;
import org.springframework.data.document.mongodb.mapping.event.AfterConvertEvent; import org.springframework.data.document.mongodb.mapping.event.AfterConvertEvent;
import org.springframework.data.document.mongodb.mapping.event.AfterLoadEvent; import org.springframework.data.document.mongodb.mapping.event.AfterLoadEvent;
import org.springframework.data.document.mongodb.mapping.event.AfterSaveEvent; import org.springframework.data.document.mongodb.mapping.event.AfterSaveEvent;
@@ -65,6 +67,7 @@ import org.springframework.data.document.mongodb.mapping.event.BeforeSaveEvent;
import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent; import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update; import org.springframework.data.document.mongodb.query.Update;
import org.springframework.data.mapping.model.MappingContext;
import org.springframework.jca.cci.core.ConnectionCallback; import org.springframework.jca.cci.core.ConnectionCallback;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -505,11 +508,19 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a List of objects. // Find methods that take a Query to express the query and that return a List of objects.
public <T> List<T> find(Query query, Class<T> targetClass) { public <T> List<T> find(Query query, Class<T> targetClass) {
return find(getDefaultCollectionName(), query, targetClass); String collName = getEntityCollection(targetClass);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
return find(collName, query, targetClass);
} }
public <T> List<T> find(Query query, Class<T> targetClass, MongoReader<T> reader) { public <T> List<T> find(Query query, Class<T> targetClass, MongoReader<T> reader) {
return find(getDefaultCollectionName(), query, targetClass, reader); String collName = getEntityCollection(targetClass);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
return find(collName, query, targetClass, reader);
} }
public <T> List<T> find(String collectionName, final Query query, Class<T> targetClass) { public <T> List<T> find(String collectionName, final Query query, Class<T> targetClass) {
@@ -556,17 +567,17 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
} }
public <T> T findAndRemove(Query query, Class<T> targetClass, public <T> T findAndRemove(Query query, Class<T> targetClass,
MongoReader<T> reader) { MongoReader<T> reader) {
return findAndRemove(getDefaultCollectionName(), query, targetClass, reader); return findAndRemove(getDefaultCollectionName(), query, targetClass, reader);
} }
public <T> T findAndRemove(String collectionName, Query query, public <T> T findAndRemove(String collectionName, Query query,
Class<T> targetClass) { Class<T> targetClass) {
return findAndRemove(collectionName, query, targetClass, null); return findAndRemove(collectionName, query, targetClass, null);
} }
public <T> T findAndRemove(String collectionName, Query query, public <T> T findAndRemove(String collectionName, Query query,
Class<T> targetClass, MongoReader<T> reader) { Class<T> targetClass, MongoReader<T> reader) {
return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), query.getSortObject(), targetClass, reader); return doFindAndRemove(collectionName, query.getQueryObject(), query.getFieldsObject(), query.getSortObject(), targetClass, reader);
} }
@@ -574,7 +585,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object) * @see org.springframework.data.document.mongodb.MongoOperations#insert(java.lang.Object)
*/ */
public void insert(Object objectToSave) { public void insert(Object objectToSave) {
insert(getRequiredDefaultCollectionName(), objectToSave); String collName = getEntityCollection(objectToSave);
if (null == collName) {
collName = getRequiredDefaultCollectionName();
}
insert(collName, objectToSave);
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -588,7 +603,11 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
* @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter) * @see org.springframework.data.document.mongodb.MongoOperations#insert(T, org.springframework.data.document.mongodb.MongoWriter)
*/ */
public <T> void insert(T objectToSave, MongoWriter<T> writer) { public <T> void insert(T objectToSave, MongoWriter<T> writer) {
insert(getDefaultCollectionName(), objectToSave, writer); String collName = getEntityCollection(objectToSave);
if (null == collName) {
collName = getDefaultCollectionName();
}
insert(collName, objectToSave, writer);
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -1084,6 +1103,36 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
return name; return name;
} }
private String getEntityCollection(Object obj) {
if (null != obj) {
return getEntityCollection(obj.getClass());
}
return null;
}
private String getEntityCollection(Class<?> clazz) {
if (mongoConverter instanceof MappingMongoConverter) {
MappingContext ctx = ((MappingMongoConverter) mongoConverter).getMappingContext();
MongoPersistentEntity entity = (MongoPersistentEntity) ctx.getPersistentEntity(clazz);
if (null != entity) {
return entity.getCollection();
}
}
// Entity hasn't yet been added, try and figure it out anyway
if (clazz.isAnnotationPresent(Document.class)) {
Document doc = clazz.getAnnotation(Document.class);
if (!"".equals(doc.collection())) {
return doc.collection();
}
// Default to simple name
return clazz.getSimpleName().toLowerCase();
}
return null;
}
/** /**
* Checks and handles any errors. * Checks and handles any errors.
* <p/> * <p/>
@@ -1240,7 +1289,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
} }
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException { public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
return collection.findAndModify(query, fields, sort, true, null, false, false); return collection.findAndModify(query, fields, sort, true, null, false, false);
} }
} }

View File

@@ -24,7 +24,7 @@ import org.springframework.data.util.TypeInformation;
/** /**
* Mongo specific {@link PersistentEntity} implementation that adds Mongo specific meta-data such as the collection name * Mongo specific {@link PersistentEntity} implementation that adds Mongo specific meta-data such as the collection name
* and the like. * and the like.
* *
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin <jbrisbin@vmware.com>
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@@ -35,25 +35,31 @@ public class MongoPersistentEntity<T> extends BasicPersistentEntity<T> {
/** /**
* Creates a new {@link MongoPersistentEntity} with the given {@link MappingContext} and {@link TypeInformation}. Will * Creates a new {@link MongoPersistentEntity} with the given {@link MappingContext} and {@link TypeInformation}. Will
* default the collection name to the entities simple type name. * default the collection name to the entities simple type name.
* *
* @param mappingContext * @param mappingContext
* @param typeInformation * @param typeInformation
*/ */
public MongoPersistentEntity(MappingContext mappingContext, TypeInformation typeInformation) { public MongoPersistentEntity(MappingContext mappingContext, TypeInformation typeInformation) {
super(mappingContext, typeInformation); super(mappingContext, typeInformation);
this.collection = typeInformation.getType().getSimpleName(); this.collection = typeInformation.getType().getSimpleName().toLowerCase();
if (typeInformation.getType().isAnnotationPresent(Document.class)) {
Document d = typeInformation.getType().getAnnotation(Document.class);
if (!"".equals(d.collection())) {
this.collection = d.collection();
}
}
} }
/** /**
* Returns the collection the entity should be stored in. * Returns the collection the entity should be stored in.
* *
* @return * @return
*/ */
public String getCollection() { public String getCollection() {
return collection; return collection;
} }
public void setCollection(String collection) { public void setCollection(String collection) {
this.collection = collection; this.collection = collection;
} }

View File

@@ -26,8 +26,8 @@ import com.mongodb.DBCollection;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.util.JSON; import com.mongodb.util.JSON;
import org.slf4j.Logger; import org.apache.commons.logging.Log;
import org.slf4j.LoggerFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.document.mongodb.CollectionCallback; import org.springframework.data.document.mongodb.CollectionCallback;
@@ -50,7 +50,7 @@ import org.springframework.util.Assert;
*/ */
public class MongoPersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent> { public class MongoPersistentEntityIndexCreator implements ApplicationListener<MappingContextEvent> {
private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class); private static final Log log = LogFactory.getLog(MongoPersistentEntityIndexCreator.class);
private Set<Class<?>> classesSeen = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>()); private Set<Class<?>> classesSeen = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>());

View File

@@ -17,8 +17,8 @@
package org.springframework.data.document.mongodb.mapping.event; package org.springframework.data.document.mongodb.mapping.event;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import org.slf4j.Logger; import org.apache.commons.logging.Log;
import org.slf4j.LoggerFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
@@ -27,7 +27,7 @@ import org.springframework.context.ApplicationListener;
*/ */
public abstract class AbstractMappingEventListener<T extends ApplicationEvent, E> implements ApplicationListener<T> { public abstract class AbstractMappingEventListener<T extends ApplicationEvent, E> implements ApplicationListener<T> {
protected final Logger log = LoggerFactory.getLogger(getClass()); protected final Log log = LogFactory.getLog(getClass());
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public void onApplicationEvent(T appEvent) { public void onApplicationEvent(T appEvent) {

View File

@@ -21,8 +21,8 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.slf4j.Logger; import org.apache.commons.logging.Log;
import org.slf4j.LoggerFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.query.Criteria; import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.CriteriaDefinition; import org.springframework.data.document.mongodb.query.CriteriaDefinition;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
@@ -42,7 +42,7 @@ import org.springframework.data.repository.query.parser.PartTree;
*/ */
class MongoQueryCreator extends AbstractQueryCreator<Query, Query> { class MongoQueryCreator extends AbstractQueryCreator<Query, Query> {
private static final Logger LOG = LoggerFactory.getLogger(MongoQueryCreator.class); private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
/** /**

View File

@@ -18,8 +18,8 @@ package org.springframework.data.document.mongodb.repository;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.slf4j.Logger; import org.apache.commons.logging.Log;
import org.slf4j.LoggerFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.MongoOperations; import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoTemplate;
@@ -43,7 +43,7 @@ import org.springframework.util.StringUtils;
/** /**
* {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances. * {@link org.springframework.beans.factory.FactoryBean} to create {@link MongoRepository} instances.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID extends Serializable> extends public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID extends Serializable> extends
@@ -54,18 +54,17 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/** /**
* Configures the {@link MongoTemplate} to be used. * Configures the {@link MongoTemplate} to be used.
* *
* @param template * @param template the template to set
* the template to set
*/ */
public void setTemplate(MongoTemplate template) { public void setTemplate(MongoTemplate template) {
this.template = template; this.template = template;
} }
/** /**
* Sets the {@link MappingContext} used with the underlying {@link MongoTemplate}. * Sets the {@link MappingContext} used with the underlying {@link MongoTemplate}.
* *
* @param mappingContext the mappingContext to set * @param mappingContext the mappingContext to set
*/ */
public void setMappingContext(MappingContext mappingContext) { public void setMappingContext(MappingContext mappingContext) {
@@ -103,7 +102,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/** /**
* Repository to create {@link MongoRepository} instances. * Repository to create {@link MongoRepository} instances.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
public static class MongoRepositoryFactory extends RepositoryFactorySupport { public static class MongoRepositoryFactory extends RepositoryFactorySupport {
@@ -116,8 +115,8 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/** /**
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoTemplate} and {@link MappingContext}. * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoTemplate} and {@link MappingContext}.
* *
* @param template must not be {@literal null} * @param template must not be {@literal null}
* @param mappingContext * @param mappingContext
*/ */
public MongoRepositoryFactory(MongoTemplate template, MappingContext mappingContext) { public MongoRepositoryFactory(MongoTemplate template, MappingContext mappingContext) {
@@ -149,7 +148,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
* (org.springframework.data.repository.support.RepositoryMetadata) * (org.springframework.data.repository.support.RepositoryMetadata)
*/ */
@Override @Override
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({"rawtypes", "unchecked"})
protected Object getTargetRepository(RepositoryMetadata metadata) { protected Object getTargetRepository(RepositoryMetadata metadata) {
Class<?> repositoryInterface = metadata.getRepositoryInterface(); Class<?> repositoryInterface = metadata.getRepositoryInterface();
@@ -183,7 +182,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/** /**
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances. * {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
private class MongoQueryLookupStrategy implements QueryLookupStrategy { private class MongoQueryLookupStrategy implements QueryLookupStrategy {
@@ -239,16 +238,16 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
return entityInformationCreator.getEntityInformation(domainClass); return entityInformationCreator.getEntityInformation(domainClass);
} }
} }
/** /**
* Simple wrapper to to create {@link MongoEntityInformation} instances based on a {@link MappingContext}. * Simple wrapper to to create {@link MongoEntityInformation} instances based on a {@link MappingContext}.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
static class EntityInformationCreator { static class EntityInformationCreator {
private final MappingContext mappingContext; private final MappingContext mappingContext;
public EntityInformationCreator(MappingContext mappingContext) { public EntityInformationCreator(MappingContext mappingContext) {
this.mappingContext = mappingContext; this.mappingContext = mappingContext;
} }
@@ -257,7 +256,7 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
if (null == mappingContext) { if (null == mappingContext) {
return new SimpleMongoEntityInformation<T, ID>(domainClass); return new SimpleMongoEntityInformation<T, ID>(domainClass);
} }
PersistentEntity<T> persistentEntity = mappingContext.getPersistentEntity(domainClass); PersistentEntity<T> persistentEntity = mappingContext.getPersistentEntity(domainClass);
if (persistentEntity == null) { if (persistentEntity == null) {
persistentEntity = mappingContext.addPersistentEntity(domainClass); persistentEntity = mappingContext.addPersistentEntity(domainClass);
@@ -269,12 +268,12 @@ public class MongoRepositoryFactoryBean<T extends MongoRepository<S, ID>, S, ID
/** /**
* {@link QueryCreationListener} inspecting {@link PartTreeMongoQuery}s and creating an index for the properties it * {@link QueryCreationListener} inspecting {@link PartTreeMongoQuery}s and creating an index for the properties it
* refers to. * refers to.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
private static class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> { private static class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
private static final Logger LOG = LoggerFactory.getLogger(IndexEnsuringQueryCreationListener.class); private static final Log LOG = LogFactory.getLog(IndexEnsuringQueryCreationListener.class);
private final MongoOperations operations; private final MongoOperations operations;
public IndexEnsuringQueryCreationListener(MongoOperations operations) { public IndexEnsuringQueryCreationListener(MongoOperations operations) {

View File

@@ -18,9 +18,9 @@ package org.springframework.data.document.mongodb.repository;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.query.BasicQuery; import org.springframework.data.document.mongodb.query.BasicQuery;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
@@ -33,7 +33,7 @@ import org.springframework.data.document.mongodb.query.Query;
public class StringBasedMongoQuery extends AbstractMongoQuery { public class StringBasedMongoQuery extends AbstractMongoQuery {
private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)"); private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
private static final Logger LOG = LoggerFactory.getLogger(StringBasedMongoQuery.class); private static final Log LOG = LogFactory.getLog(StringBasedMongoQuery.class);
private final String query; private final String query;
private final String fieldSpec; private final String fieldSpec;
@@ -71,7 +71,9 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
query = new BasicQuery(queryString); query = new BasicQuery(queryString);
} }
LOG.debug("Created query {}", query.getQueryObject()); if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Created query {}", query.getQueryObject()));
}
return query; return query;
} }

View File

@@ -21,7 +21,7 @@ import org.bson.types.ObjectId;
/** /**
* @author Jon Brisbin <jbrisbin@vmware.com> * @author Jon Brisbin <jbrisbin@vmware.com>
*/ */
@Document @Document(collection = "places")
public class Location { public class Location {
private ObjectId id; private ObjectId id;

View File

@@ -17,8 +17,8 @@
package org.springframework.data.document.mongodb.mapping; package org.springframework.data.document.mongodb.mapping;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import org.slf4j.Logger; import org.apache.commons.logging.Log;
import org.slf4j.LoggerFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener; import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEventListener;
/** /**
@@ -26,7 +26,7 @@ import org.springframework.data.document.mongodb.mapping.event.AbstractMappingEv
*/ */
public class MappingEventsListener<MongoMappingEvent> extends AbstractMappingEventListener { public class MappingEventsListener<MongoMappingEvent> extends AbstractMappingEventListener {
private Logger log = LoggerFactory.getLogger(getClass()); private Log log = LogFactory.getLog(getClass());
@Override @Override
public void onBeforeConvert(Object source) { public void onBeforeConvert(Object source) {

View File

@@ -24,11 +24,12 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.mongodb.DB;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoDbUtils; import org.springframework.data.document.mongodb.MongoDbUtils;
@@ -41,7 +42,8 @@ import org.springframework.data.document.mongodb.query.Query;
*/ */
public class MappingTests { public class MappingTests {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbUtils.class); private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class);
private final String[] collectionsToDrop = new String[]{"person", "personmapproperty", "personpojo", "personcustomidname", "account"};
ApplicationContext applicationContext; ApplicationContext applicationContext;
MongoTemplate template; MongoTemplate template;
@@ -50,7 +52,10 @@ public class MappingTests {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Mongo mongo = new Mongo(); Mongo mongo = new Mongo();
mongo.getDB("database").getCollection("person").drop(); DB db = mongo.getDB("database");
for (String coll : collectionsToDrop) {
db.getCollection(coll).drop();
}
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml"); applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
template = applicationContext.getBean(MongoTemplate.class); template = applicationContext.getBean(MongoTemplate.class);
mappingContext = applicationContext.getBean(MongoMappingContext.class); mappingContext = applicationContext.getBean(MongoMappingContext.class);
@@ -58,6 +63,9 @@ public class MappingTests {
@Test @Test
public void testPersonPojo() throws Exception { public void testPersonPojo() throws Exception {
// POJOs aren't auto-detected, have to add manually
mappingContext.addPersistentEntity(PersonPojo.class);
LOGGER.info("about to create new personpojo"); LOGGER.info("about to create new personpojo");
PersonPojo p = new PersonPojo(12345, "Person", "Pojo"); PersonPojo p = new PersonPojo(12345, "Person", "Pojo");
LOGGER.info("about to insert"); LOGGER.info("about to insert");
@@ -73,6 +81,9 @@ public class MappingTests {
@Test @Test
public void testPersonWithCustomIdName() { public void testPersonWithCustomIdName() {
// POJOs aren't auto-detected, have to add manually
mappingContext.addPersistentEntity(PersonCustomIdName.class);
PersonCustomIdName p = new PersonCustomIdName(123456, "Custom", "Id"); PersonCustomIdName p = new PersonCustomIdName(123456, "Custom", "Id");
template.insert(p); template.insert(p);
@@ -163,15 +174,15 @@ public class MappingTests {
} }
@Test @Test
public void testPrimitives() { public void testPrimitivesAndCustomCollectionName() {
Location loc = new Location( Location loc = new Location(
new double[]{1.0, 2.0}, new double[]{1.0, 2.0},
new int[]{1, 2, 3, 4}, new int[]{1, 2, 3, 4},
new float[]{1.0f, 2.0f} new float[]{1.0f, 2.0f}
); );
template.insert("locations", loc); template.insert(loc);
List<Location> result = template.find("locations", new Query(Criteria.where("_id").is(loc.getId())), Location.class); List<Location> result = template.find("places", new Query(Criteria.where("_id").is(loc.getId())), Location.class);
assertThat(result.size(), is(1)); assertThat(result.size(), is(1));
} }

View File

@@ -33,5 +33,4 @@ Import-Template:
org.apache.commons.collections15.*;version="[4.0.0,5.0.0)", org.apache.commons.collections15.*;version="[4.0.0,5.0.0)",
org.apache.commons.logging.*;version="[1.1.1, 2.0.0)", org.apache.commons.logging.*;version="[1.1.1, 2.0.0)",
org.bson.*;version="0", org.bson.*;version="0",
org.slf4j.*;version="[1.5.0,1.6.0)",
org.w3c.dom.*;version="0" org.w3c.dom.*;version="0"