Merge pull request #233 from alex-semenyuk/master

Queries in Spring Data MongoDB
This commit is contained in:
Eugen 2015-08-08 03:17:37 -07:00
commit 2e7f18bb41
8 changed files with 361 additions and 50 deletions

View File

@ -1,27 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
<classpathentry including="**/*.java" kind="src" path="target/generated-sources/java"/>
<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
<classpathentry kind="var" path="M2_REPO/javax/inject/javax.inject/1/javax.inject-1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/data/spring-data-mongodb/1.7.1.RELEASE/spring-data-mongodb-1.7.1.RELEASE.jar" sourcepath="M2_REPO/org/springframework/data/spring-data-mongodb/1.7.1.RELEASE/spring-data-mongodb-1.7.1.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/4.0.9.RELEASE/spring-tx-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/4.0.9.RELEASE/spring-beans-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/4.0.9.RELEASE/spring-core-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/4.0.9.RELEASE/spring-context-4.0.9.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-context/4.0.9.RELEASE/spring-context-4.0.9.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/4.0.9.RELEASE/spring-aop-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar" sourcepath="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/4.0.9.RELEASE/spring-expression-4.0.9.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/data/spring-data-commons/1.10.1.RELEASE/spring-data-commons-1.10.1.RELEASE.jar" sourcepath="M2_REPO/org/springframework/data/spring-data-commons/1.10.1.RELEASE/spring-data-commons-1.10.1.RELEASE-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/jcl-over-slf4j/1.7.12/jcl-over-slf4j-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/org/mongodb/mongo-java-driver/2.13.0/mongo-java-driver-2.13.0.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar" sourcepath="M2_REPO/junit/junit/4.11/junit-4.11-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar" sourcepath="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-library/1.3/hamcrest-library-1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-test/4.1.7.RELEASE/spring-test-4.1.7.RELEASE.jar"/>
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar"/>
<classpathentry kind="var" path="M2_REPO/org/slf4j/log4j-over-slf4j/1.7.12/log4j-over-slf4j-1.7.12.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-mongodb/3.6.6/querydsl-mongodb-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-core/3.6.6/querydsl-core-3.6.6.jar" sourcepath="M2_REPO/com/mysema/querydsl/querydsl-core/3.6.6/querydsl-core-3.6.6-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/google/guava/guava/18.0/guava-18.0.jar" sourcepath="M2_REPO/com/google/guava/guava/18.0/guava-18.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/commons/mysema-commons-lang/0.2.4/mysema-commons-lang-0.2.4.jar"/>
<classpathentry kind="var" path="M2_REPO/com/infradna/tool/bridge-method-annotation/1.13/bridge-method-annotation-1.13.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-apt/3.6.6/querydsl-apt-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/querydsl/querydsl-codegen/3.6.6/querydsl-codegen-3.6.6.jar"/>
<classpathentry kind="var" path="M2_REPO/com/mysema/codegen/codegen/0.6.7/codegen-0.6.7.jar"/>
<classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/compiler/ecj/4.3.1/ecj-4.3.1.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,23 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-data-mongodb</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
<name>spring-data-mongodb</name>
<comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -14,6 +14,12 @@
<artifactId>spring-data-mongodb</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@ -66,10 +72,37 @@
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${mysema.maven.version}</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
@ -85,10 +118,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.7.1.RELEASE</org.springframework.data.version>
<org.springframework.version>4.1.7.RELEASE</org.springframework.version>
<org.springframework.version>4.2.0.RELEASE</org.springframework.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<rest-assured.version>2.4.1</rest-assured.version>
<querydsl.version>3.6.6</querydsl.version>
<mysema.maven.version>1.1.3</mysema.maven.version>
<org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version>

View File

@ -3,6 +3,9 @@ package org.baeldung.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.mysema.query.annotations.QueryEntity;
@QueryEntity
@Document
public class User {

View File

@ -1,8 +1,19 @@
package org.baeldung.repository;
import org.baeldung.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface UserRepository extends MongoRepository<User, String> {
//
import org.baeldung.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User> {
@Query("{ 'name' : ?0 }")
List<User> findUsersByName(String name);
@Query(value = "{'age':?0}", fields = "{ 'name' : 1}")
List<String> findUsersByAgeAndReturnNames(int age);
List<User> findByAgeBetween(int ageGT, int ageLT);
}

View File

@ -9,7 +9,8 @@
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>
<context:annotation-config/>
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost"/>
</bean>
@ -22,5 +23,8 @@
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate"/>
<bean class="org.baeldung.event.BeforeConvertListener">
</bean>
</beans>

View File

@ -0,0 +1,131 @@
package org.baeldung.mongotemplate;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.config.MongoConfig;
import org.baeldung.model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class MongoTemplateQueryIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Before
public void testSetup() {
mongoTemplate.createCollection(User.class);
}
@After
public void tearDown() {
mongoTemplate.dropCollection(User.class);
}
@Test
public void givenUsersExist_whenFindingUserWithAgeLessThan50AndGreateThan20_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(55);
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("age").lt(50).gt(20));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUserWithNameStartWithA_thenUsersAreFound() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
Query query = new Query();
query.addCriteria(Criteria.where("name").regex("^A"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
final Pageable pageableRequest = new PageRequest(0, 2);
Query query = new Query();
query.with(pageableRequest);
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUsersAndSortThem_thenUsersAreFoundAndSorted() {
User user = new User();
user.setName("Eric");
user.setAge(45);
mongoTemplate.insert(user);
user = new User();
user.setName("Antony");
user.setAge(33);
mongoTemplate.insert(user);
user = new User();
user.setName("Alice");
user.setAge(35);
mongoTemplate.insert(user);
Query query = new Query();
query.with(new Sort(Sort.Direction.ASC, "age"));
List<User> users = mongoTemplate.find(query, User.class);
assertThat(users.size(), is(3));
}
}

View File

@ -0,0 +1,120 @@
package org.baeldung.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.baeldung.config.MongoConfig;
import org.baeldung.model.QUser;
import org.baeldung.model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mysema.query.types.Predicate;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class MongoRepositoryQueryIntegrationTest {
@Autowired
private UserRepository userRepository;
@Autowired
private MongoOperations mongoOps;
@Before
public void testSetup() {
mongoOps.createCollection(User.class);
}
@After
public void tearDown() {
mongoOps.dropCollection(User.class);
}
@Test
public void givenUsersExist_whenFindingUsersByName_thenUsersAreFound() {
User user = new User();
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setName("Jim");
mongoOps.insert(user);
List<User> users = userRepository.findUsersByName("Jon");
assertThat(users.size(), is(2));
}
@Test
public void givenUsersExist_whenFindingUsersByAgeAndReturningNames_thenUsersAreFoundAndReturnedNames() {
User user = new User();
user.setName("Jon");
user.setAge(20);
mongoOps.insert(user);
user = new User();
user.setName("Jon");
user.setAge(30);
mongoOps.insert(user);
user = new User();
user.setName("Jim");
user.setAge(40);
mongoOps.insert(user);
List<String> users = userRepository.findUsersByAgeAndReturnNames(30);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersWithAgeCreaterThanAndLessThan_thenUsersAreFound() {
User user = new User();
user.setAge(20);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(50);
user.setName("Jon");
mongoOps.insert(user);
user = new User();
user.setAge(33);
user.setName("Jim");
mongoOps.insert(user);
List<User> users = userRepository.findByAgeBetween(26, 40);
assertThat(users.size(), is(1));
}
@Test
public void givenUsersExist_whenFindingUsersAndCount_thenUserAreFound() {
User user = new User();
user.setName("Alex");
mongoOps.insert(user);
user = new User();
user.setName("Mat");
mongoOps.insert(user);
QUser qUser = new QUser("user");
Predicate predicate = qUser.name.eq("Alex");
List<User> users = (List<User>) userRepository.findAll(predicate);
assertThat(users.size(), is(1));
}
}