Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -69,6 +69,7 @@
|
||||
<module>spring-boot-persistence-mongodb</module>
|
||||
<module>spring-boot-persistence-mongodb-2</module>
|
||||
<module>spring-boot-persistence-mongodb-3</module>
|
||||
<module>spring-boot-persistence-mongodb-4</module>
|
||||
<module>spring-data-arangodb</module>
|
||||
<!--<module>spring-data-cassandra</module>--> <!-- failing after upgrading to jdk17. JDK 17 compatibility in progress CASSANDRA-16895 -->
|
||||
<module>spring-data-cassandra-test</module>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
# Relevant Articles
|
||||
|
||||
- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime)
|
||||
- [A Guide to @DBRef in MongoDB](https://www.baeldung.com/spring-mongodb-dbref-annotation)
|
||||
- More articles: [[<--prev]](../spring-boot-persistence-mongodb-3)
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-boot-persistence-mongodb-4</artifactId>
|
||||
<name>spring-boot-persistence-mongodb-4</name>
|
||||
<description>This is simple boot application for Spring boot persistence mongodb test</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!-- MongoDB -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+1
-2
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.mongodb.dbref;
|
||||
|
||||
import com.baeldung.mongodb.dbref.repository.PersonRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -7,8 +8,6 @@ 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 {
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Document(collection = "Person")
|
||||
public class Person {
|
||||
|
||||
+1
-2
@@ -1,8 +1,7 @@
|
||||
package com.baeldung.mongodb.dbref.repository;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.mongodb.dbref.model.Person;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface PersonRepository extends MongoRepository<Person, String> {
|
||||
|
||||
+4
-5
@@ -1,15 +1,14 @@
|
||||
package com.baeldung.zoneddatetime.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeReadConverter;
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeWriteConverter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
|
||||
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeReadConverter;
|
||||
import com.baeldung.zoneddatetime.converter.ZonedDateTimeWriteConverter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@EnableMongoRepositories(basePackages = { "com.baeldung" })
|
||||
public class MongoConfig extends AbstractMongoClientConfiguration {
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
package com.baeldung.zoneddatetime.repository;
|
||||
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
|
||||
public interface ActionRepository extends MongoRepository<Action, String> { }
|
||||
+1
@@ -0,0 +1 @@
|
||||
spring.application.name=spring-boot-persistence-mongodb-4
|
||||
+11
-12
@@ -1,11 +1,11 @@
|
||||
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 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;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -14,12 +14,11 @@ 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;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
+3
-4
@@ -1,5 +1,8 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
import com.baeldung.zoneddatetime.config.MongoConfig;
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
import com.baeldung.zoneddatetime.repository.ActionRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
@@ -10,10 +13,6 @@ import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.zoneddatetime.config.MongoConfig;
|
||||
import com.baeldung.zoneddatetime.model.Action;
|
||||
import com.baeldung.zoneddatetime.repository.ActionRepository;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
spring.mongodb.embedded.version=4.4.9
|
||||
@@ -4,8 +4,6 @@
|
||||
- [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb)
|
||||
- [Upload and Retrieve Files Using MongoDB and Spring Boot](https://www.baeldung.com/spring-boot-mongodb-upload-file)
|
||||
- [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs)
|
||||
- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime)
|
||||
- [A Guide to @DBRef in MongoDB](https://www.baeldung.com/spring-mongodb-dbref-annotation)
|
||||
- [Import Data to MongoDB From JSON File Using Java](https://www.baeldung.com/java-import-json-mongodb)
|
||||
- [Spring Data MongoDB – Configure Connection](https://www.baeldung.com/spring-data-mongodb-connection)
|
||||
- More articles: [[next-->]](../spring-boot-persistence-mongodb-2)
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.projection.model;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document(collection = "student")
|
||||
public class Student {
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
private Long age;
|
||||
|
||||
public Student(String id, String name, Long age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Long getAge() {
|
||||
return age;
|
||||
}
|
||||
public void setAge(Long age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.projection.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.mongodb.repository.Aggregation;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.projection.model.Student;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface StudentRepository extends MongoRepository<Student, String> {
|
||||
@Aggregation(pipeline = {
|
||||
"{ '$skip': ?0 }",
|
||||
"{ '$limit': ?1 }"
|
||||
})
|
||||
List<Student> findAll(Long skip, Long limit);
|
||||
@Aggregation(pipeline = {
|
||||
"{ '$match': { 'age': ?0 } }",
|
||||
"{ $skip: ?1 }",
|
||||
"{ $limit: ?2 }"
|
||||
})
|
||||
List<Student> findAllByAgeCriteria(Long age, Long skip, Long limit);
|
||||
@Aggregation(pipeline = {
|
||||
"{ '$match': { 'id' : ?0 } }",
|
||||
"{ '$sort' : { 'id' : 1 } }",
|
||||
"{ '$skip' : ?1 }",
|
||||
"{ '$limit' : ?2 }"
|
||||
})
|
||||
List<Student> findByStudentId(final String studentId, Long skip, Long limit);
|
||||
Page<Student> findAll(Pageable pageable);
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.projection;
|
||||
|
||||
import com.baeldung.projection.config.ProjectionConfig;
|
||||
import com.baeldung.projection.model.Student;
|
||||
import com.baeldung.projection.repository.StudentRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = ProjectionConfig.class)
|
||||
public class StudentIntegrationTest {
|
||||
@Autowired
|
||||
private StudentRepository studentRepository;
|
||||
private List<Student> studentList;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Student student1 = new Student("A", "Abraham", 15L);
|
||||
Student student2 = new Student("B", "Usman", 30L);
|
||||
Student student3 = new Student("C", "David", 20L);
|
||||
Student student4 = new Student("D", "Tina", 45L);
|
||||
Student student5 = new Student("E", "Maria", 33L);
|
||||
|
||||
studentList = Arrays.asList(student1, student2, student3, student4, student5);
|
||||
studentRepository.saveAll(studentList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRetrievingAllStudents_thenReturnsCorrectNumberOfRecords() {
|
||||
// WHEN
|
||||
List<Student> result = studentRepository.findAll(0L, 5L);
|
||||
// THEN
|
||||
assertEquals(5, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLimitingAndSkipping_thenReturnsLimitedStudents() {
|
||||
// WHEN
|
||||
List<Student> result = studentRepository.findAll(3L, 2L);
|
||||
// THEN
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("Tina", result.get(0).getName());
|
||||
assertEquals("Maria", result.get(1).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFilteringByAge_thenReturnsStudentsMatchingCriteria() {
|
||||
// WHEN
|
||||
List<Student> result = studentRepository.findAllByAgeCriteria(30L, 0L, 10L);
|
||||
// THEN
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Usman", result.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingById_thenReturnsMatchingStudent() {
|
||||
// WHEN
|
||||
List<Student> result = studentRepository.findByStudentId("A", 0L, 5L);
|
||||
// THEN
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Abraham", result.get(0).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindByStudentIdUsingPageable_thenReturnsPageOfStudents() {
|
||||
// GIVEN
|
||||
Sort sort = Sort.by(Sort.Direction.DESC, "id");
|
||||
Pageable pageable = PageRequest.of(0, 5, sort);
|
||||
|
||||
// WHEN
|
||||
Page<Student> resultPage = studentRepository.findAll(pageable);
|
||||
|
||||
// THEN
|
||||
assertEquals(5, resultPage.getTotalElements());
|
||||
assertEquals("Maria", resultPage.getContent().get(0).getName());
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -30,7 +31,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -46,47 +47,11 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>3.2.0-SNAPSHOT</spring-boot.version>
|
||||
<junit-jupiter.version>5.10.0</junit-jupiter.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring-boot.version>3.2.4</spring-boot.version> <!-- Requires at least spring framework version 6.1.X to work -->
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+1
-2
@@ -4,8 +4,7 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "com.baledung.jdbcclient")
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung")
|
||||
public class JdbcClientDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user