From 13cf53aaf428e3881ba4609eafc3fa0f16600ecc Mon Sep 17 00:00:00 2001 From: coach88 Date: Thu, 13 Aug 2015 14:11:37 +0300 Subject: [PATCH 1/6] Example with adding indexes from code and like annotation. --- .../main/java/org/baeldung/model/User.java | 17 +++++++++++ .../UserRepositoryIntegrationTest.java | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java index 8cf516762d..d314263b4e 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -1,7 +1,12 @@ package org.baeldung.model; import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.index.CompoundIndex; +import org.springframework.data.mongodb.core.index.CompoundIndexes; +import org.springframework.data.mongodb.core.index.Indexed; +import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.Field; import com.mysema.query.annotations.QueryEntity; @@ -13,6 +18,10 @@ public class User { private String id; private String name; private Integer age; + @DBRef + @Indexed + @Field("email") + private EmailAddress emailAddress; public String getId() { return id; @@ -37,4 +46,12 @@ public class User { public void setAge(final Integer age) { this.age = age; } + + public EmailAddress getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(EmailAddress emailAddress) { + this.emailAddress = emailAddress; + } } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java index 009191f03e..ea8a6b8e7e 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat; import java.util.List; import org.baeldung.config.MongoConfig; +import org.baeldung.model.EmailAddress; import org.baeldung.model.User; import org.junit.After; import org.junit.Before; @@ -16,7 +17,10 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.index.Index; +import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; @@ -145,4 +149,30 @@ public class UserRepositoryIntegrationTest { assertThat(users.size(), is(1)); assertThat(page.getTotalPages(), is(2)); } + + @Test + public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { + User user = new User(); + user.setName("Brendan"); + EmailAddress emailAddress = new EmailAddress("a@gmail.com"); + mongoOps.insert(emailAddress); + user.setEmailAddress(emailAddress); + mongoOps.insert(user); + + List indexInfos = mongoOps.indexOps("user").getIndexInfo(); + + assertThat(indexInfos.size(), is(1)); + } + + @Test + public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() { + User user = new User(); + user.setName("Brendan"); + mongoOps.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); + mongoOps.insert(user); + + List indexInfos = mongoOps.indexOps("user").getIndexInfo(); + + assertThat(indexInfos.size(), is(2)); + } } From 3e4f69fb747c1c429a3fef23ce656f13f78ad7f6 Mon Sep 17 00:00:00 2001 From: coach88 Date: Fri, 14 Aug 2015 18:50:33 +0300 Subject: [PATCH 2/6] Added event on saving User cascade save EmailAddress --- .../java/org/baeldung/config/MongoConfig.java | 7 ++++ .../main/java/org/baeldung/model/User.java | 9 ++-- .../DocumentQueryIntegrationTest.java | 18 ++++---- .../MongoTemplateQueryIntegrationTest.java | 42 +++++++++++++++++++ .../UserRepositoryIntegrationTest.java | 29 ------------- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index d9214c182b..7a789b1859 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -1,5 +1,7 @@ package org.baeldung.config; +import org.baeldung.event.CascadingMongoEventListener; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; @@ -25,4 +27,9 @@ public class MongoConfig extends AbstractMongoConfiguration { public String getMappingBasePackage() { return "org.baeldung"; } + + @Bean + public CascadingMongoEventListener cascadingMongoEventListener(){ + return new CascadingMongoEventListener(); + } } diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java index d314263b4e..77d53ffbe9 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -1,8 +1,8 @@ package org.baeldung.model; +import org.baeldung.annotation.CascadeSave; import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.index.CompoundIndex; -import org.springframework.data.mongodb.core.index.CompoundIndexes; +import org.springframework.data.mongodb.core.index.IndexDirection; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; @@ -16,11 +16,14 @@ public class User { @Id private String id; + @Indexed(direction = IndexDirection.ASCENDING) private String name; + private Integer age; @DBRef - @Indexed @Field("email") + @CascadeSave + private EmailAddress emailAddress; public String getId() { diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java index 83867dd423..cf46c6ed6e 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/DocumentQueryIntegrationTest.java @@ -7,6 +7,7 @@ import java.util.Iterator; import java.util.List; import org.baeldung.config.MongoConfig; +import org.baeldung.model.EmailAddress; import org.baeldung.model.User; import org.junit.After; import org.junit.Before; @@ -31,11 +32,14 @@ public class DocumentQueryIntegrationTest { @Before public void testSetup() { - mongoTemplate.createCollection(User.class); + if (!mongoTemplate.collectionExists(User.class)) { + mongoTemplate.createCollection(User.class); + } } @After public void tearDown() { + mongoTemplate.dropCollection(EmailAddress.class); mongoTemplate.dropCollection(User.class); } @@ -102,7 +106,7 @@ public class DocumentQueryIntegrationTest { query.addCriteria(Criteria.where("name").regex("^A")); List users = mongoTemplate.find(query, User.class); - + assertThat(users.size(), is(2)); } @@ -127,7 +131,7 @@ public class DocumentQueryIntegrationTest { query.addCriteria(Criteria.where("name").regex("c$")); List users = mongoTemplate.find(query, User.class); - + assertThat(users.size(), is(1)); } @@ -153,7 +157,7 @@ public class DocumentQueryIntegrationTest { query.with(pageableRequest); List users = mongoTemplate.find(query, User.class); - + assertThat(users.size(), is(2)); } @@ -178,11 +182,11 @@ public class DocumentQueryIntegrationTest { query.with(new Sort(Sort.Direction.ASC, "age")); List users = mongoTemplate.find(query, User.class); - + Iterator iter = users.iterator(); - assertThat(users.size(), is(3)); + assertThat(users.size(), is(3)); assertThat(iter.next().getName(), is("Antony")); assertThat(iter.next().getName(), is("Alice")); - assertThat(iter.next().getName(), is("Eric")); + assertThat(iter.next().getName(), is("Eric")); } } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java index 2a8a2fd71b..71ec9277b5 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat; import java.util.List; import org.baeldung.config.MongoConfig; +import org.baeldung.model.EmailAddress; import org.baeldung.model.User; import org.junit.After; import org.junit.Before; @@ -15,7 +16,10 @@ 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.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.index.Index; +import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; @@ -128,4 +132,42 @@ public class MongoTemplateQueryIntegrationTest { List users = mongoTemplate.find(query, User.class); assertThat(users.size(), is(3)); } + + @Test + public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { + User user = new User(); + user.setName("Brendan"); + EmailAddress emailAddress = new EmailAddress(); + emailAddress.setValue("a@gmail.com"); + user.setEmailAddress(emailAddress); + mongoTemplate.insert(user); + + List indexInfos = mongoTemplate.indexOps("user").getIndexInfo(); + + assertThat(indexInfos.size(), is(1)); + } + + @Test + public void givenExisted() { + User user = new User(); + user.setName("Brendan"); + EmailAddress emailAddress = new EmailAddress(); + emailAddress.setValue("b@gmail.com"); + user.setEmailAddress(emailAddress); + mongoTemplate.insert(user); + + assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Brendan")), User.class).getEmailAddress().getValue(), is("b@gmail.com")); + } + + @Test + public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() { + User user = new User(); + user.setName("Brendan"); + mongoTemplate.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); + mongoTemplate.insert(user); + + List indexInfos = mongoTemplate.indexOps("user").getIndexInfo(); + + assertThat(indexInfos.size(), is(2)); + } } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java index ea8a6b8e7e..7524aacda2 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java @@ -6,7 +6,6 @@ import static org.junit.Assert.assertThat; import java.util.List; import org.baeldung.config.MongoConfig; -import org.baeldung.model.EmailAddress; import org.baeldung.model.User; import org.junit.After; import org.junit.Before; @@ -17,10 +16,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoOperations; -import org.springframework.data.mongodb.core.index.Index; -import org.springframework.data.mongodb.core.index.IndexInfo; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.test.context.ContextConfiguration; @@ -149,30 +145,5 @@ public class UserRepositoryIntegrationTest { assertThat(users.size(), is(1)); assertThat(page.getTotalPages(), is(2)); } - - @Test - public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { - User user = new User(); - user.setName("Brendan"); - EmailAddress emailAddress = new EmailAddress("a@gmail.com"); - mongoOps.insert(emailAddress); - user.setEmailAddress(emailAddress); - mongoOps.insert(user); - - List indexInfos = mongoOps.indexOps("user").getIndexInfo(); - assertThat(indexInfos.size(), is(1)); - } - - @Test - public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() { - User user = new User(); - user.setName("Brendan"); - mongoOps.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); - mongoOps.insert(user); - - List indexInfos = mongoOps.indexOps("user").getIndexInfo(); - - assertThat(indexInfos.size(), is(2)); - } } From 236b808d0545114eabc98a9eb954f978ef032ecf Mon Sep 17 00:00:00 2001 From: coach88 Date: Sat, 15 Aug 2015 00:11:19 +0300 Subject: [PATCH 3/6] Minor cleanup with tests Added converter on saving in db user --- .../org/baeldung/annotation/CascadeSave.java | 12 ++++++ .../java/org/baeldung/config/MongoConfig.java | 15 ++++++++ .../converter/UserWriterConverter.java | 25 +++++++++++++ .../event/CascadingMongoEventListener.java | 37 +++++++++++++++++++ .../baeldung/event/DbRefFieldCallback.java | 22 +++++++++++ .../java/org/baeldung/model/EmailAddress.java | 34 +++++++++++++++++ .../main/java/org/baeldung/model/User.java | 12 +++++- .../MongoTemplateQueryIntegrationTest.java | 6 ++- .../repository/BaseQueryIntegrationTest.java | 4 +- .../UserRepositoryIntegrationTest.java | 7 ++-- 10 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/annotation/CascadeSave.java create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/event/DbRefFieldCallback.java create mode 100644 spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java diff --git a/spring-data-mongodb/src/main/java/org/baeldung/annotation/CascadeSave.java b/spring-data-mongodb/src/main/java/org/baeldung/annotation/CascadeSave.java new file mode 100644 index 0000000000..9fba40245b --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/annotation/CascadeSave.java @@ -0,0 +1,12 @@ +package org.baeldung.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface CascadeSave { + +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index 7a789b1859..8433e67850 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -1,9 +1,16 @@ package org.baeldung.config; +import java.util.ArrayList; +import java.util.List; + + +import org.baeldung.converter.UserWriterConverter; import org.baeldung.event.CascadingMongoEventListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; +import org.springframework.data.mongodb.core.convert.CustomConversions; +import org.springframework.core.convert.converter.Converter; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; @@ -13,6 +20,8 @@ import com.mongodb.MongoClient; @EnableMongoRepositories(basePackages = "org.baeldung.repository") public class MongoConfig extends AbstractMongoConfiguration { + private List> converters = new ArrayList>(); + @Override protected String getDatabaseName() { return "test"; @@ -32,4 +41,10 @@ public class MongoConfig extends AbstractMongoConfiguration { public CascadingMongoEventListener cascadingMongoEventListener(){ return new CascadingMongoEventListener(); } + + @Override + public CustomConversions customConversions() { + converters.add(new UserWriterConverter()); + return new CustomConversions(converters); + } } diff --git a/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java b/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java new file mode 100644 index 0000000000..18f2314c6b --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/converter/UserWriterConverter.java @@ -0,0 +1,25 @@ +package org.baeldung.converter; + +import org.springframework.stereotype.Component; +import org.baeldung.model.User; +import org.springframework.core.convert.converter.Converter; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; + +@Component +public class UserWriterConverter implements Converter { + @Override + public DBObject convert(User user) { + DBObject dbObject = new BasicDBObject(); + dbObject.put("name", user.getName()); + dbObject.put("age", user.getAge()); + if (user.getEmailAddress() != null) { + DBObject emailDbObject = new BasicDBObject(); + emailDbObject.put("value", user.getEmailAddress().getValue()); + dbObject.put("email", emailDbObject); + } + dbObject.removeField("_class"); + return dbObject; + } +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java new file mode 100644 index 0000000000..e461160eb4 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java @@ -0,0 +1,37 @@ +package org.baeldung.event; + +import org.baeldung.annotation.CascadeSave; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoOperations; +import org.springframework.data.mongodb.core.mapping.DBRef; +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Field; + +public class CascadingMongoEventListener extends AbstractMongoEventListener { + @Autowired + private MongoOperations mongoOperations; + + @Override + public void onBeforeConvert(final Object source) { + ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() { + + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { + ReflectionUtils.makeAccessible(field); + + if (field.isAnnotationPresent(DBRef.class) && field.isAnnotationPresent(CascadeSave.class)) { + final Object fieldValue = field.get(source); + + if (fieldValue != null) { + DbRefFieldCallback callback = new DbRefFieldCallback(); + + ReflectionUtils.doWithFields(fieldValue.getClass(), callback); + + mongoOperations.save(fieldValue); + } + } + } + }); + } +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/DbRefFieldCallback.java b/spring-data-mongodb/src/main/java/org/baeldung/event/DbRefFieldCallback.java new file mode 100644 index 0000000000..66f797042a --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/DbRefFieldCallback.java @@ -0,0 +1,22 @@ +package org.baeldung.event; + +import java.lang.reflect.Field; + +import org.springframework.data.annotation.Id; +import org.springframework.util.ReflectionUtils; + +public class DbRefFieldCallback implements ReflectionUtils.FieldCallback { + private boolean idFound; + + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { + ReflectionUtils.makeAccessible(field); + + if (field.isAnnotationPresent(Id.class)) { + idFound = true; + } + } + + public boolean isIdFound() { + return idFound; + } +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java b/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java new file mode 100644 index 0000000000..f54aea0e78 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java @@ -0,0 +1,34 @@ +package org.baeldung.model; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document +public class EmailAddress { + @Id + private String id; + private String value; + + public EmailAddress(){ + } + + public EmailAddress(String value) { + this.value = value; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java index 77d53ffbe9..ff4eacd0c2 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -20,11 +20,21 @@ public class User { private String name; private Integer age; + @DBRef @Field("email") @CascadeSave - private EmailAddress emailAddress; + + public User(){ + } + + + public User(String name, Integer age, String value){ + this.name = name; + this.age = age; + this.emailAddress = new EmailAddress(value); + } public String getId() { return id; diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java index 71ec9277b5..c23e981294 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java @@ -34,7 +34,9 @@ public class MongoTemplateQueryIntegrationTest { @Before public void testSetup() { - mongoTemplate.createCollection(User.class); + if (!mongoTemplate.collectionExists(User.class)) { + mongoTemplate.createCollection(User.class); + } } @After @@ -148,7 +150,7 @@ public class MongoTemplateQueryIntegrationTest { } @Test - public void givenExisted() { + public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() { User user = new User(); user.setName("Brendan"); EmailAddress emailAddress = new EmailAddress(); diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java index 459716c7ca..8572cc858e 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/BaseQueryIntegrationTest.java @@ -17,7 +17,9 @@ public class BaseQueryIntegrationTest { @Before public void testSetup() { - mongoOps.createCollection(User.class); + if (!mongoOps.collectionExists(User.class)) { + mongoOps.createCollection(User.class); + } } @After diff --git a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java index 7524aacda2..53cadc09bc 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java @@ -34,7 +34,9 @@ public class UserRepositoryIntegrationTest { @Before public void testSetup() { - mongoOps.createCollection(User.class); + if (!mongoOps.collectionExists(User.class)) { + mongoOps.createCollection(User.class); + } } @After @@ -67,12 +69,11 @@ public class UserRepositoryIntegrationTest { mongoOps.insert(user); user = mongoOps.findOne(Query.query(Criteria.where("name").is("Jack")), User.class); - final String id = user.getId(); user.setName("Jim"); userRepository.save(user); - assertThat(mongoOps.findOne(Query.query(Criteria.where("id").is(id)), User.class).getName(), is("Jim")); + assertThat(mongoOps.findAll(User.class).size(), is(2)); } @Test From 971d62568eb0d4f0eb528fffaf65e08c33ab819b Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sat, 15 Aug 2015 16:34:42 +0200 Subject: [PATCH 4/6] Added xml support for event listener and made minor cleanup --- .../java/org/baeldung/config/MongoConfig.java | 17 ++++++++--------- ....java => CascadeSaveMongoEventListener.java} | 4 ++-- ...RefFieldCallback.java => FieldCallback.java} | 2 +- .../java/org/baeldung/model/EmailAddress.java | 8 ++++---- .../src/main/java/org/baeldung/model/User.java | 9 ++++----- .../src/main/resources/mongoConfig.xml | 11 ++++++++--- 6 files changed, 27 insertions(+), 24 deletions(-) rename spring-data-mongodb/src/main/java/org/baeldung/event/{CascadingMongoEventListener.java => CascadeSaveMongoEventListener.java} (88%) rename spring-data-mongodb/src/main/java/org/baeldung/event/{DbRefFieldCallback.java => FieldCallback.java} (86%) diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java index 8433e67850..4b776af5b6 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/MongoConfig.java @@ -3,9 +3,8 @@ package org.baeldung.config; import java.util.ArrayList; import java.util.List; - import org.baeldung.converter.UserWriterConverter; -import org.baeldung.event.CascadingMongoEventListener; +import org.baeldung.event.CascadeSaveMongoEventListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; @@ -20,7 +19,7 @@ import com.mongodb.MongoClient; @EnableMongoRepositories(basePackages = "org.baeldung.repository") public class MongoConfig extends AbstractMongoConfiguration { - private List> converters = new ArrayList>(); + private List> converters = new ArrayList>(); @Override protected String getDatabaseName() { @@ -36,15 +35,15 @@ public class MongoConfig extends AbstractMongoConfiguration { public String getMappingBasePackage() { return "org.baeldung"; } - + @Bean - public CascadingMongoEventListener cascadingMongoEventListener(){ - return new CascadingMongoEventListener(); + public CascadeSaveMongoEventListener cascadingMongoEventListener() { + return new CascadeSaveMongoEventListener(); } - + @Override public CustomConversions customConversions() { - converters.add(new UserWriterConverter()); - return new CustomConversions(converters); + converters.add(new UserWriterConverter()); + return new CustomConversions(converters); } } diff --git a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java similarity index 88% rename from spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java rename to spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java index e461160eb4..ad09f1ac04 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/event/CascadingMongoEventListener.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/event/CascadeSaveMongoEventListener.java @@ -9,7 +9,7 @@ import org.springframework.util.ReflectionUtils; import java.lang.reflect.Field; -public class CascadingMongoEventListener extends AbstractMongoEventListener { +public class CascadeSaveMongoEventListener extends AbstractMongoEventListener { @Autowired private MongoOperations mongoOperations; @@ -24,7 +24,7 @@ public class CascadingMongoEventListener extends AbstractMongoEventListener - + + - - + + + + + + \ No newline at end of file From c5fc2a714e2198e6b3b85f9d0817a08a93e7a4a4 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sat, 15 Aug 2015 20:30:19 +0200 Subject: [PATCH 5/6] Minor cleanup --- .../main/java/org/baeldung/model/EmailAddress.java | 11 ++--------- .../src/main/java/org/baeldung/model/User.java | 9 --------- .../MongoTemplateQueryIntegrationTest.java | 6 +++--- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java b/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java index 095ce40c01..6db7d160d7 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/EmailAddress.java @@ -9,18 +9,11 @@ public class EmailAddress { private String id; private String value; - public EmailAddress() { - } - - public EmailAddress(String value) { - this.value = value; - } - public String getId() { return id; } - public void setId(String id) { + public void setId(final String id) { this.id = id; } @@ -28,7 +21,7 @@ public class EmailAddress { return value; } - public void setValue(String value) { + public void setValue(final String value) { this.value = value; } } diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java index 70d97d3417..e5977c658a 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -26,15 +26,6 @@ public class User { @CascadeSave private EmailAddress emailAddress; - public User() { - } - - public User(String name, Integer age, String value) { - this.name = name; - this.age = age; - this.emailAddress = new EmailAddress(value); - } - public String getId() { return id; } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java index c23e981294..f0185e97a4 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java @@ -137,7 +137,7 @@ public class MongoTemplateQueryIntegrationTest { @Test public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() { - User user = new User(); + final User user = new User(); user.setName("Brendan"); EmailAddress emailAddress = new EmailAddress(); emailAddress.setValue("a@gmail.com"); @@ -151,7 +151,7 @@ public class MongoTemplateQueryIntegrationTest { @Test public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() { - User user = new User(); + final User user = new User(); user.setName("Brendan"); EmailAddress emailAddress = new EmailAddress(); emailAddress.setValue("b@gmail.com"); @@ -163,7 +163,7 @@ public class MongoTemplateQueryIntegrationTest { @Test public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() { - User user = new User(); + final User user = new User(); user.setName("Brendan"); mongoTemplate.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC)); mongoTemplate.insert(user); From 4f55bef4c345db4816739ed188f7f456adbad04f Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Sat, 15 Aug 2015 20:36:08 +0200 Subject: [PATCH 6/6] Minor cleanup --- spring-data-mongodb/src/main/resources/mongoConfig.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/resources/mongoConfig.xml b/spring-data-mongodb/src/main/resources/mongoConfig.xml index 7772b351e5..1a9d161ad6 100644 --- a/spring-data-mongodb/src/main/resources/mongoConfig.xml +++ b/spring-data-mongodb/src/main/resources/mongoConfig.xml @@ -25,7 +25,7 @@ - +