[BAEL-5373] A Guide to @DBRef in MongoDB (#11909)

* Add implementation for BAEL-5373.

* Fix test class name.

* Change collection names to be aligned with article.
This commit is contained in:
AttilaUhrin 2022-03-18 04:16:47 +01:00 committed by GitHub
parent 5f00d426c5
commit d01b0734d3
6 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package com.baeldung.mongodb.dbref;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DbRefApplication {
public static void main(String... args) {
SpringApplication.run(DbRefApplication.class, args);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.mongodb.dbref;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.baeldung.mongodb.dbref.repository.PersonRepository;
@Component
public class DbRefTester implements ApplicationRunner {
private static final Logger logger = LoggerFactory.getLogger(DbRefTester.class);
@Autowired
private PersonRepository personRepository;
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("{}", personRepository.findAll());
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.mongodb.dbref.model;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "Person")
public class Person {
@Id
private String id;
private String name;
@DBRef
private List<Pet> pets;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Pet> getPets() {
return pets;
}
public void setPets(List<Pet> pets) {
this.pets = pets;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", pets=" + pets + "]";
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.mongodb.dbref.model;
public class Pet {
private String id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Pet [id=" + id + ", name=" + name + "]";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.mongodb.dbref.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.baeldung.mongodb.dbref.model.Person;
public interface PersonRepository extends MongoRepository<Person, String> {
}

View File

@ -0,0 +1,74 @@
package com.baeldung.mongodb.dbref;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.mongodb.dbref.model.Person;
import com.baeldung.mongodb.dbref.model.Pet;
import com.baeldung.mongodb.dbref.repository.PersonRepository;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
public class DbRefIntegrationTest {
@Autowired
PersonRepository personRepository;
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void givenPetsAndPersonInDatabase_whenListPersons_thenReferenceIsFetched() {
// given
DBObject catInDatabase = BasicDBObjectBuilder.start()
.add("name", "Loki")
.get();
DBObject dogInDatabase = BasicDBObjectBuilder.start()
.add("name", "Max")
.get();
mongoTemplate.save(catInDatabase, "Cat");
mongoTemplate.save(dogInDatabase, "Dog");
List<DBRef> petsReference = new ArrayList<DBRef>();
petsReference.add(new DBRef("Cat", catInDatabase.get("_id")));
petsReference.add(new DBRef("Dog", dogInDatabase.get("_id")));
DBObject personInDatabase = BasicDBObjectBuilder.start()
.add("name", "Bob")
.add("pets", petsReference)
.get();
mongoTemplate.save(personInDatabase, "Person");
// when
List<Person> persons = personRepository.findAll();
// then
assertThat(persons).hasSize(1);
Person person = persons.get(0);
assertEquals("Bob", person.getName());
List<Pet> pets = person.getPets();
assertThat(pets).hasSize(2);
assertThat(pets).anyMatch(pet -> "Loki".equals(pet.getName()));
assertThat(pets).anyMatch(pet -> "Max".equals(pet.getName()));
}
}