upgrade spring-data-mongodb

This commit is contained in:
DOHA 2018-10-04 22:18:12 +03:00
parent faae433c40
commit e65d77a47a
8 changed files with 158 additions and 108 deletions

View File

@ -8,9 +8,9 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-4</artifactId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-4</relativePath>
<relativePath>../parent-spring-5</relativePath>
</parent>
<dependencies>
@ -19,6 +19,28 @@
<artifactId>spring-data-mongodb</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Lovelace-M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.2.0.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
@ -48,6 +70,17 @@
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
@ -70,10 +103,11 @@
</build>
<properties>
<org.springframework.data.version>1.10.4.RELEASE</org.springframework.data.version>
<rest-assured.version>2.9.0</rest-assured.version>
<org.springframework.data.version>2.1.0.RELEASE</org.springframework.data.version>
<querydsl.version>4.1.4</querydsl.version>
<mysema.maven.version>1.1.3</mysema.maven.version>
<spring.version>5.1.0.RELEASE</spring.version>
</properties>
</project>

View File

@ -1,20 +1,22 @@
package com.baeldung.config;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.baeldung.converter.UserWriterConverter;
import com.baeldung.event.CascadeSaveMongoEventListener;
import com.baeldung.event.UserCascadeSaveMongoEventListener;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.converter.UserWriterConverter;
import com.baeldung.event.CascadeSaveMongoEventListener;
import com.baeldung.event.UserCascadeSaveMongoEventListener;
import com.mongodb.MongoClient;
@Configuration
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
@ -28,7 +30,7 @@ public class MongoConfig extends AbstractMongoConfiguration {
}
@Override
public Mongo mongo() throws Exception {
public MongoClient mongoClient() {
return new MongoClient("127.0.0.1", 27017);
}
@ -57,4 +59,10 @@ public class MongoConfig extends AbstractMongoConfiguration {
public GridFsTemplate gridFsTemplate() throws Exception {
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
}
@Bean
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
}
}

View File

@ -1,18 +1,18 @@
package com.baeldung.config;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.MongoClient;
@Configuration
@EnableMongoRepositories(basePackages = "com.baeldung.repository")
public class SimpleMongoConfig {
@Bean
public Mongo mongo() throws Exception {
public MongoClient mongo() throws Exception {
return new MongoClient("localhost");
}

View File

@ -1,13 +1,14 @@
package com.baeldung.repository;
import com.baeldung.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import java.util.List;
public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User> {
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import com.baeldung.model.User;
public interface UserRepository extends MongoRepository<User, String>, QuerydslPredicateExecutor<User> {
@Query("{ 'name' : ?0 }")
List<User> findUsersByName(String name);

View File

@ -3,17 +3,17 @@
xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:annotation-config/>
<mongo:mongo id="mongo" host="localhost" />
<mongo:mongo-client id="mongoClient" host="localhost" />
<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongo" />
<mongo:db-factory id="mongoDbFactory" dbname="test" mongo-ref="mongoClient" />
<bean id="gridFsTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<constructor-arg ref="mongoDbFactory" />
@ -27,12 +27,12 @@
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<mongo:repositories base-package="org.baeldung.repository" mongo-template-ref="mongoTemplate"/>
<mongo:repositories base-package="com.baeldung.repository" mongo-template-ref="mongoTemplate"/>
<bean class="org.baeldung.event.CascadeSaveMongoEventListener">
<bean class="com.baeldung.event.CascadeSaveMongoEventListener">
</bean>
<mongo:mapping-converter id="mongoConverter" base-package="org.baeldung.converter">
<mongo:mapping-converter id="mongoConverter" base-package="com.baeldung.converter">
<mongo:custom-converters base-package="org.baeldung.converter"/>
</mongo:mapping-converter>

View File

@ -1,12 +1,24 @@
package com.baeldung.aggregation;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;
import com.baeldung.aggregation.model.StatePopulation;
import com.baeldung.config.MongoConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.bson.Document;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@ -26,23 +38,13 @@ import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
import com.baeldung.aggregation.model.StatePopulation;
import com.baeldung.config.MongoConfig;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
@ -140,13 +142,13 @@ public class ZipsAggregationLiveTest {
Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast);
AggregationResults<DBObject> result = mongoTemplate.aggregate(aggregation, "zips", DBObject.class);
DBObject dbObject = result.getUniqueMappedResult();
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation, "zips", Document.class);
Document document = result.getUniqueMappedResult();
assertEquals("DC", dbObject.get("minZipState"));
assertEquals(24, dbObject.get("minZipCount"));
assertEquals("TX", dbObject.get("maxZipState"));
assertEquals(1671, dbObject.get("maxZipCount"));
assertEquals("DC", document.get("minZipState"));
assertEquals(24, document.get("minZipCount"));
assertEquals("TX", document.get("maxZipState"));
assertEquals(1671, document.get("maxZipCount"));
}
}

View File

@ -1,8 +1,19 @@
package com.baeldung.gridfs;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFSDBFile;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -16,18 +27,9 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.model.GridFSFile;
@ContextConfiguration("file:src/main/resources/mongoConfig.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@ -40,8 +42,9 @@ public class GridFSLiveTest {
@After
public void tearDown() {
List<GridFSDBFile> fileList = gridFsTemplate.find(null);
for (GridFSDBFile file : fileList) {
List<GridFSFile> fileList = new ArrayList<GridFSFile>();
gridFsTemplate.find(new Query()).into(fileList);
for (GridFSFile file : fileList) {
gridFsTemplate.delete(new Query(Criteria.where("filename").is(file.getFilename())));
}
}
@ -54,7 +57,7 @@ public class GridFSLiveTest {
String id = "";
try {
inputStream = new FileInputStream("src/main/resources/test.png");
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
@ -75,10 +78,10 @@ public class GridFSLiveTest {
DBObject metaData = new BasicDBObject();
metaData.put("user", "alex");
InputStream inputStream = null;
String id = "";
ObjectId id = null;
try {
inputStream = new FileInputStream("src/main/resources/test.png");
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData);
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
@ -91,22 +94,22 @@ public class GridFSLiveTest {
}
}
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
GridFSFile gridFSFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
assertNotNull(gridFSDBFile);
assertNotNull(gridFSDBFile.getInputStream());
assertThat(gridFSDBFile.numChunks(), is(1));
assertThat(gridFSDBFile.containsField("filename"), is(true));
assertThat(gridFSDBFile.get("filename"), is("test.png"));
assertThat(gridFSDBFile.getId(), is(id));
assertThat(gridFSDBFile.keySet().size(), is(9));
assertNotNull(gridFSDBFile.getMD5());
assertNotNull(gridFSDBFile.getUploadDate());
assertNull(gridFSDBFile.getAliases());
assertNotNull(gridFSDBFile.getChunkSize());
assertThat(gridFSDBFile.getContentType(), is("image/png"));
assertThat(gridFSDBFile.getFilename(), is("test.png"));
assertThat(gridFSDBFile.getMetaData().get("user"), is("alex"));
assertNotNull(gridFSFile);
// assertNotNull(gridFSFile.getInputStream());
// assertThat(gridFSFile.numChunks(), is(1));
// assertThat(gridFSFile.containsField("filename"), is(true));
assertThat(gridFSFile.getFilename(), is("test.png"));
assertThat(gridFSFile.getObjectId(), is(id));
// assertThat(gridFSFile.keySet().size(), is(9));
// assertNotNull(gridFSFile.getMD5());
assertNotNull(gridFSFile.getUploadDate());
// assertNull(gridFSFile.getAliases());
assertNotNull(gridFSFile.getChunkSize());
assertThat(gridFSFile.getMetadata().get("_contentType"), is("image/png"));
assertThat(gridFSFile.getFilename(), is("test.png"));
assertThat(gridFSFile.getMetadata().get("user"), is("alex"));
}
@Test
@ -133,10 +136,11 @@ public class GridFSLiveTest {
}
}
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(null);
List<GridFSFile> gridFSFiles = new ArrayList<GridFSFile>();
gridFsTemplate.find(new Query()).into(gridFSFiles);
assertNotNull(gridFSDBFiles);
assertThat(gridFSDBFiles.size(), is(2));
assertNotNull(gridFSFiles);
assertThat(gridFSFiles.size(), is(2));
}
@Test
@ -163,10 +167,11 @@ public class GridFSLiveTest {
}
}
List<GridFSDBFile> gridFSDBFiles = gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex")));
List<GridFSFile> gridFSFiles = new ArrayList<GridFSFile>();
gridFsTemplate.find(new Query(Criteria.where("metadata.user").is("alex"))).into(gridFSFiles);
assertNotNull(gridFSDBFiles);
assertThat(gridFSDBFiles.size(), is(1));
assertNotNull(gridFSFiles);
assertThat(gridFSFiles.size(), is(1));
}
@Test
@ -177,7 +182,7 @@ public class GridFSLiveTest {
String id = "";
try {
inputStream = new FileInputStream("src/main/resources/test.png");
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {
@ -203,7 +208,7 @@ public class GridFSLiveTest {
String id = "";
try {
inputStream = new FileInputStream("src/main/resources/test.png");
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).getId().toString();
id = gridFsTemplate.store(inputStream, "test.png", "image/png", metaData).toString();
} catch (FileNotFoundException ex) {
logger.error("File not found", ex);
} finally {

View File

@ -5,8 +5,6 @@ import static org.junit.Assert.assertThat;
import java.util.List;
import com.baeldung.config.MongoConfig;
import com.baeldung.model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -22,6 +20,9 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.config.MongoConfig;
import com.baeldung.model.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MongoConfig.class)
public class UserRepositoryLiveTest {
@ -72,8 +73,7 @@ public class UserRepositoryLiveTest {
user.setName("Jim");
userRepository.save(user);
assertThat(mongoOps.findAll(User.class).size(), is(2));
assertThat(mongoOps.findAll(User.class).size(), is(1));
}
@Test
@ -94,7 +94,7 @@ public class UserRepositoryLiveTest {
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Chris")), User.class);
final User foundUser = userRepository.findOne(user.getId());
final User foundUser = userRepository.findById(user.getId()).get();
assertThat(user.getName(), is(foundUser.getName()));
}
@ -106,7 +106,7 @@ public class UserRepositoryLiveTest {
mongoOps.insert(user);
user = mongoOps.findOne(Query.query(Criteria.where("name").is("Harris")), User.class);
final boolean isExists = userRepository.exists(user.getId());
final boolean isExists = userRepository.existsById(user.getId());
assertThat(isExists, is(true));
}