File tree Expand file tree Collapse file tree 6 files changed +559
-0
lines changed
persistence-modules/spring-data-mongodb Expand file tree Collapse file tree 6 files changed +559
-0
lines changed Original file line number Diff line number Diff line change 46
46
<version >${embed.mongo.version} </version >
47
47
<scope >test</scope >
48
48
</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 >
49
65
</dependencies >
50
66
51
67
<build >
66
82
</execution >
67
83
</executions >
68
84
</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 >
69
93
</plugins >
70
94
</build >
71
95
72
96
<properties >
97
+ <spring-boot .version>3.4.1</spring-boot .version>
98
+ <instancio .version>5.3.0</instancio .version>
73
99
<querydsl .version>5.1.0</querydsl .version>
74
100
<mysema .maven.version>1.1.3</mysema .maven.version>
75
101
<projectreactor .version>3.5.4</projectreactor .version>
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments