Skip to content

Commit 008c490

Browse files
codebase/introduction-to-spring-data-mongodb [BAEL-8677] [Improvement] (#18236)
* add domain model and repository * fix: Document parameter * update active datatype and add repository methods * add test cases for AuthorRepository * update assertions * update assertion * add main application * fix: bean conflicts * add test cases for MongoTemplate
1 parent 04f8ecb commit 008c490

File tree

6 files changed

+559
-0
lines changed

6 files changed

+559
-0
lines changed

persistence-modules/spring-data-mongodb/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@
4646
<version>${embed.mongo.version}</version>
4747
<scope>test</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.instancio</groupId>
51+
<artifactId>instancio-junit</artifactId>
52+
<version>${instancio.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-testcontainers</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.testcontainers</groupId>
62+
<artifactId>mongodb</artifactId>
63+
<scope>test</scope>
64+
</dependency>
4965
</dependencies>
5066

5167
<build>
@@ -66,10 +82,20 @@
6682
</execution>
6783
</executions>
6884
</plugin>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-compiler-plugin</artifactId>
88+
<configuration>
89+
<source>21</source>
90+
<target>21</target>
91+
</configuration>
92+
</plugin>
6993
</plugins>
7094
</build>
7195

7296
<properties>
97+
<spring-boot.version>3.4.1</spring-boot.version>
98+
<instancio.version>5.3.0</instancio.version>
7399
<querydsl.version>5.1.0</querydsl.version>
74100
<mysema.maven.version>1.1.3</mysema.maven.version>
75101
<projectreactor.version>3.5.4</projectreactor.version>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
/**
8+
* Main Spring Boot class that scans only the repository package to avoid
9+
* bean conflicts with shared codebase's manual MongoDB configuration.
10+
* This ensures that MongoDataAutoConfiguration handles MongoDB setup while preventing
11+
* duplicate bean definitions from external config classes.
12+
*/
13+
@SpringBootApplication
14+
@ComponentScan(basePackages = "com.baeldung.repository")
15+
public class Application {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
21+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.index.Indexed;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
import org.springframework.data.mongodb.core.mapping.Field;
7+
8+
import java.util.UUID;
9+
10+
@Document(collection = "authors")
11+
public class Author {
12+
13+
@Id
14+
private UUID id;
15+
16+
@Field(name = "full_name")
17+
private String name;
18+
19+
@Indexed(unique = true)
20+
private String email;
21+
22+
@Field(name = "article_count")
23+
private Integer articleCount;
24+
25+
private Boolean active;
26+
27+
public UUID getId() {
28+
return id;
29+
}
30+
31+
public void setId(UUID id) {
32+
this.id = id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String getEmail() {
44+
return email;
45+
}
46+
47+
public void setEmail(String email) {
48+
this.email = email;
49+
}
50+
51+
public Integer getArticleCount() {
52+
return articleCount;
53+
}
54+
55+
public void setArticleCount(Integer articleCount) {
56+
this.articleCount = articleCount;
57+
}
58+
59+
public Boolean isActive() {
60+
return active;
61+
}
62+
63+
public void setActive(Boolean active) {
64+
this.active = active;
65+
}
66+
67+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.model.Author;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
import org.springframework.data.mongodb.repository.Query;
6+
7+
import java.util.List;
8+
import java.util.Optional;
9+
import java.util.UUID;
10+
11+
public interface AuthorRepository extends MongoRepository<Author, UUID> {
12+
13+
Optional<Author> findByEmail(String email);
14+
15+
List<Author> findByActiveTrueAndArticleCountGreaterThanEqual(int articleCount);
16+
17+
@Query("{ 'article_count': { $gte: ?0, $lte: ?1 }, 'active': true }")
18+
List<Author> findActiveAuthorsInArticleRange(int minArticles, int maxArticles);
19+
20+
@Query(value = "{ 'active': true }", fields = "{ 'email': 1 }")
21+
List<Author> findActiveAuthorEmails();
22+
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.test.context.TestConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.test.context.DynamicPropertyRegistrar;
6+
import org.testcontainers.containers.MongoDBContainer;
7+
import org.testcontainers.utility.DockerImageName;
8+
9+
@TestConfiguration(proxyBeanMethods = false)
10+
public class TestcontainersConfiguration {
11+
12+
@Bean
13+
MongoDBContainer mongoDbContainer() {
14+
return new MongoDBContainer(DockerImageName.parse("mongo:8"));
15+
}
16+
17+
@Bean
18+
DynamicPropertyRegistrar dynamicPropertyRegistrar(MongoDBContainer mongoDbContainer) {
19+
return registry -> {
20+
registry.add("spring.data.mongodb.uri", mongoDbContainer::getConnectionString);
21+
registry.add("spring.data.mongodb.database", () -> "technical-content-management");
22+
};
23+
}
24+
25+
}

0 commit comments

Comments
 (0)