Added event on saving User cascade save EmailAddress
This commit is contained in:
parent
13cf53aaf4
commit
3e4f69fb74
@ -1,5 +1,7 @@
|
|||||||
package org.baeldung.config;
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import org.baeldung.event.CascadingMongoEventListener;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||||
@ -25,4 +27,9 @@ public class MongoConfig extends AbstractMongoConfiguration {
|
|||||||
public String getMappingBasePackage() {
|
public String getMappingBasePackage() {
|
||||||
return "org.baeldung";
|
return "org.baeldung";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CascadingMongoEventListener cascadingMongoEventListener(){
|
||||||
|
return new CascadingMongoEventListener();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package org.baeldung.model;
|
package org.baeldung.model;
|
||||||
|
|
||||||
|
import org.baeldung.annotation.CascadeSave;
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.mongodb.core.index.CompoundIndex;
|
import org.springframework.data.mongodb.core.index.IndexDirection;
|
||||||
import org.springframework.data.mongodb.core.index.CompoundIndexes;
|
|
||||||
import org.springframework.data.mongodb.core.index.Indexed;
|
import org.springframework.data.mongodb.core.index.Indexed;
|
||||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||||
import org.springframework.data.mongodb.core.mapping.Document;
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
@ -16,11 +16,14 @@ public class User {
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
private String id;
|
private String id;
|
||||||
|
@Indexed(direction = IndexDirection.ASCENDING)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private Integer age;
|
private Integer age;
|
||||||
@DBRef
|
@DBRef
|
||||||
@Indexed
|
|
||||||
@Field("email")
|
@Field("email")
|
||||||
|
@CascadeSave
|
||||||
|
|
||||||
private EmailAddress emailAddress;
|
private EmailAddress emailAddress;
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
|
@ -7,6 +7,7 @@ import java.util.Iterator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
|
import org.baeldung.model.EmailAddress;
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -31,11 +32,14 @@ public class DocumentQueryIntegrationTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void testSetup() {
|
public void testSetup() {
|
||||||
mongoTemplate.createCollection(User.class);
|
if (!mongoTemplate.collectionExists(User.class)) {
|
||||||
|
mongoTemplate.createCollection(User.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
|
mongoTemplate.dropCollection(EmailAddress.class);
|
||||||
mongoTemplate.dropCollection(User.class);
|
mongoTemplate.dropCollection(User.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
|
import org.baeldung.model.EmailAddress;
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
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.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
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.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.Criteria;
|
||||||
import org.springframework.data.mongodb.core.query.Query;
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
@ -128,4 +132,42 @@ public class MongoTemplateQueryIntegrationTest {
|
|||||||
List<User> users = mongoTemplate.find(query, User.class);
|
List<User> users = mongoTemplate.find(query, User.class);
|
||||||
assertThat(users.size(), is(3));
|
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<IndexInfo> 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<IndexInfo> indexInfos = mongoTemplate.indexOps("user").getIndexInfo();
|
||||||
|
|
||||||
|
assertThat(indexInfos.size(), is(2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import static org.junit.Assert.assertThat;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
import org.baeldung.model.EmailAddress;
|
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
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.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.domain.Sort;
|
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.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.Criteria;
|
||||||
import org.springframework.data.mongodb.core.query.Query;
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
@ -150,29 +146,4 @@ public class UserRepositoryIntegrationTest {
|
|||||||
assertThat(page.getTotalPages(), is(2));
|
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<IndexInfo> 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<IndexInfo> indexInfos = mongoOps.indexOps("user").getIndexInfo();
|
|
||||||
|
|
||||||
assertThat(indexInfos.size(), is(2));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user