#8 - Add sample for @Meta usage.

Renamed the geo-spatial example to example only as it not only covers geo-spatial samples. Added advanced example to show the usage of the @Meta annotation.

Original pull request: #11.
This commit is contained in:
Christoph Strobl
2014-09-01 12:35:42 +02:00
committed by Oliver Gierke
parent ecadc65483
commit 6fb5259e37
13 changed files with 218 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ We have separate folders for the samples of individual modules:
## Spring Data MongoDB
* `geo-spatial` - Example project for general repository functionality (including geo-spatial functionality) and Querydsl integration
* `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics.
* `aggregation` - Example project to showcase the MongoDB aggregation framework support.
* `text-search` - Example project showing usage of MongoDB text search feature.

View File

@@ -2,9 +2,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-mongodb-geospatial</artifactId>
<artifactId>spring-data-mongodb-example</artifactId>
<name>Spring Data MongoDB - Geo-spatial &amp; Querydsl</name>
<name>Spring Data MongoDB - Example</name>
<parent>
<groupId>org.springframework.data.examples</groupId>

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.advanced;
import java.util.List;
import org.springframework.data.mongodb.repository.Meta;
import example.springdata.mongodb.customer.Customer;
import example.springdata.mongodb.customer.CustomerRepository;
/**
* Repository interface to manage {@link Customer} instances.
*
* @author Christoph Strobl
*/
public interface AdvancedRepository extends CustomerRepository {
String META_COMMENT = "s2gx-2014-rocks!";
/**
* Derived query using {@code $comment} meta attribute for quick lookup. <br />
* Have a look at the {@literal mongodb shell} and execute:
*
* <pre>
* <code>
* db['system.profile'].find({'query.$comment':'s2gx-2014-rocks!'})
* </code>
* </pre>
*
* @param firstname
* @return
*/
@Meta(comment = META_COMMENT)
List<Customer> findByFirstname(String firstname);
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.advanced;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoOperations;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
/**
* Test configuration to connect to a MongoDB named "test" and using a {@link MongoClient} with profiling enabled.
*
* @author Christoph Strobl
*/
@Configuration
@EnableAutoConfiguration
class ApplicationConfiguration {
static final String SYSTEM_PROFILE_DB = "system.profile";
@Autowired MongoOperations operations;
/**
* Initialize db instance with defaults.
*/
@PostConstruct
public void initializeWithDefaults() {
// Enable profiling
setProfilingLevel(2);
}
/**
* Clean up resources on shutdown
*/
@PreDestroy
public void cleanUpWhenShuttingDown() {
// Disable profiling
setProfilingLevel(0);
if (operations.collectionExists(SYSTEM_PROFILE_DB)) {
operations.dropCollection(SYSTEM_PROFILE_DB);
}
}
private void setProfilingLevel(int level) {
operations.executeCommand(new BasicDBObject("profile", level));
}
}

View File

@@ -28,4 +28,4 @@ import com.mongodb.MongoClient;
*/
@Configuration
@EnableAutoConfiguration
public class ApplicationConfiguration {}
class ApplicationConfiguration {}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.advanced;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Meta;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import example.springdata.mongodb.customer.Customer;
/**
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
public class AdvancedIntegrationTests {
@Autowired AdvancedRepository repository;
@Autowired MongoOperations operations;
Customer dave, oliver, carter;
@Before
public void setUp() {
repository.deleteAll();
dave = repository.save(new Customer("Dave", "Matthews"));
oliver = repository.save(new Customer("Oliver August", "Matthews"));
carter = repository.save(new Customer("Carter", "Beauford"));
}
/**
* This test demonstrates usage of {@code $comment} {@link Meta} usage. One can also enable profiling using
* {@code --profile=2} when starting {@literal mongod}.
* <p>
* <strong>NOTE</strong>: Requires MongoDB v. 2.6.4+
*/
@Test
public void findByFirstnameUsingMetaAttributes() {
// execute derived finder method just to get the comment in the profile log
repository.findByFirstname(dave.getFirstname());
// execute another finder without meta attributes that should not be picked up
repository.findByLastname(dave.getLastname(), new Sort("firstname"));
DBCursor cursor = operations.getCollection(ApplicationConfiguration.SYSTEM_PROFILE_DB).find(
new BasicDBObject("query.$comment", AdvancedRepository.META_COMMENT));
while (cursor.hasNext()) {
DBObject dbo = cursor.next();
DBObject query = (DBObject) dbo.get("query");
assertThat(query.containsField("$comment"), is(true));
}
}
}

View File

@@ -0,0 +1,5 @@
/**
* Package showing usage of Spring Data abstractions for special (advanced) MongoDB operations.
*/
package example.springdata.mongodb.advanced;

View File

@@ -0,0 +1,5 @@
/**
* Package showing basic usage of Spring Data MongoDB Repositories.
*/
package example.springdata.mongodb.customer;

View File

@@ -17,7 +17,7 @@
<inceptionYear>2011-2014</inceptionYear>
<modules>
<module>geo-spatial</module>
<module>example</module>
<module>aggregation</module>
<module>text-search</module>
</modules>