update docs

look for entity collection on finds in mongotemplate
This commit is contained in:
Mark Pollack
2011-04-08 18:20:32 -04:00
parent 827d0c52ee
commit 8192db49c5
3 changed files with 79 additions and 16 deletions

View File

@@ -468,12 +468,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// Find methods that take a Query to express the query and that return a single object.
public <T> T findOne(Query query, Class<T> targetClass) {
return findOne(getDefaultCollectionName(), query, targetClass);
return findOne(getEntityCollection(targetClass), query, targetClass);
}
public <T> T findOne(Query query, Class<T> targetClass,
MongoReader<T> reader) {
return findOne(getDefaultCollectionName(), query, targetClass, reader);
return findOne(getEntityCollection(targetClass), query, targetClass, reader);
}
public <T> T findOne(String collectionName, Query query,
@@ -536,12 +536,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
// also removed from the collection in the database.
public <T> T findAndRemove(Query query, Class<T> targetClass) {
return findAndRemove(getDefaultCollectionName(), query, targetClass);
return findAndRemove(getEntityCollection(targetClass), query, targetClass);
}
public <T> T findAndRemove(Query query, Class<T> targetClass,
MongoReader<T> reader) {
return findAndRemove(getDefaultCollectionName(), query, targetClass, reader);
return findAndRemove(getEntityCollection(targetClass), query, targetClass, reader);
}
public <T> T findAndRemove(String collectionName, Query query,

View File

@@ -1,17 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<chapter id="mongo.logging">
<title>Logging support</title>
<para>Logging support introduction.
</para>
<para>An appender for Log4j is provided in the maven module
"spring-data-mongodb-log4j". Note, there is no dependency on other Spring
Mongo modules, only the MongoDB driver.</para>
<section id="mongodb:logging-configuration">
<title>MongoDB Log4j Configuration</title>
<para>Log4j...
</para>
<para>Here is an example configuration</para>
<programlisting>log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.springframework.data.document.mongodb.log4j.MongoLog4jAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - &lt;%m&gt;%n
log4j.appender.stdout.host = localhost
log4j.appender.stdout.port = 27017
log4j.appender.stdout.database = logs
log4j.appender.stdout.collectionPattern = %X{year}%X{month}
log4j.appender.stdout.applicationId = my.application
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
log4j.category.org.apache.activemq=ERROR
log4j.category.org.springframework.batch=DEBUG
log4j.category.org.springframework.data.document.mongodb=DEBUG
log4j.category.org.springframework.transaction=INFO</programlisting>
<para>The important configuration to look at aside from host and port is
the database and collectionPattern. The variables year, month, day and
hour are available for you to use in forming a collection name. This is to
support the common convention of grouping log information in a collection
that corresponds to a specific time period, for example a collection per
day.</para>
<para>There is also an applicationId which is put into the stored message.
The document stored from logging as the following keys: level, name,
applicationId, timestamp, properties, traceback, and message. </para>
</section>
</chapter>

View File

@@ -105,11 +105,7 @@
&lt;artifactId&gt;cglib&lt;/artifactId&gt;
&lt;version&gt;2.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;log4j&lt;/groupId&gt;
&lt;artifactId&gt;log4j&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;</programlisting>
</programlisting>
<para>The cglib dependency is there as we will use Spring's Java
configuration style. Also change the version of Spring in the pom.xml to
@@ -117,6 +113,11 @@
<programlisting lang="xml">&lt;spring.framework.version&gt;3.0.5.RELEASE&lt;/spring.framework.version&gt;</programlisting>
<para>You may also want to set the logging level to DEBUG to see some
additional information, edit the log4j.properties file and add</para>
<programlisting>log4j.category.org.springframework.data.document.mongodb=DEBUG</programlisting>
<para>Next, in the org.spring.mongodb package in the sr/ctest/java
directory create a class as shown below.</para>
@@ -145,7 +146,7 @@ public class MongoConfig extends AbstractMongoConfiguration {
<para>Then create a simple Person class to persist</para>
<programlisting>package org.spring.mongodb;
<programlisting language="java">package org.spring.mongodb;
public class Person {
@@ -175,7 +176,42 @@ public class Person {
<para>And a main application to run</para>
<programlisting></programlisting>
<programlisting language="java">package org.spring.mongodb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class);
MongoOperations mongoOps = ctx.getBean(MongoOperations.class);
mongoOps.insert(new Person("1234", "Joe"));
log.info(mongoOps.findOne(new Query(Criteria.where("name").is("Joe")), Person.class));
}
}</programlisting>
<para>This will produce the following output</para>
<programlisting>MongoPersistentEntityIndexCreator] - &lt;Analyzing class class org.spring.mongodb.Person for index information.&gt;
LoggingEventListener] - &lt;onBeforeConvert: Person [id=1234, name=Joe]&gt;
LoggingEventListener] - &lt;onBeforeSave: Person [id=1234, name=Joe], { "_id" : "1234" , "name" : "Joe"}&gt;
MongoTemplate] - &lt;insert DBObject: { "_id" : "1234" , "name" : "Joe"}&gt;
LoggingEventListener] - &lt;onAfterSave: Person [id=1234, name=Joe], { "_id" : "1234" , "name" : "Joe"}&gt;
MongoTemplate] - &lt;findOne using query: { "name" : "Joe"} in db.collection: database.person&gt;
LoggingEventListener] - &lt;onAfterLoad: { "_id" : "1234" , "name" : "Joe"}&gt;
LoggingEventListener] - &lt;onAfterConvert: { "_id" : "1234" , "name" : "Joe"}, Person [id=1234, name=Joe]&gt;
MongoApp] - &lt;Person [id=1234, name=Joe]&gt;</programlisting>
</section>
<section id="mongodb-connectors">