forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5400 - add spring-data-mongodb-2 module (eugenp#12340)
* fix: add spring-data-mongodb-2 module * fix: missing module in parent pom
- Loading branch information
1 parent
e31a4df
commit 50e503c
Showing
11 changed files
with
764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
========= | ||
|
||
## Spring Data MongoDB 2 | ||
|
||
### Relevant Articles: | ||
- [Return Only Specific Fields for a Query in Spring Data MongoDB](https://www.baeldung.com/mongodb-return-specific-fields) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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-data-mongodb-2</artifactId> | ||
<name>spring-data-mongodb-2</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-spring-5</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<relativePath>../../parent-spring-5</relativePath> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-mongodb</artifactId> | ||
<version>${org.springframework.data.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongodb-driver-sync</artifactId> | ||
<version>${mongodb-driver.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${spring.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<artifactId>commons-logging</artifactId> | ||
<groupId>commons-logging</groupId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>de.flapdoodle.embed</groupId> | ||
<artifactId>de.flapdoodle.embed.mongo</artifactId> | ||
<version>${embed.mongo.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<org.springframework.data.version>3.0.3.RELEASE</org.springframework.data.version> | ||
<mongodb-driver.version>4.0.5</mongodb-driver.version> | ||
<embed.mongo.version>3.2.6</embed.mongo.version> | ||
</properties> | ||
|
||
</project> |
46 changes: 46 additions & 0 deletions
46
...ce-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/InStock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.baeldung.projection.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class InStock { | ||
|
||
private String wareHouse; | ||
private Integer quantity; | ||
|
||
public InStock(String wareHouse, int quantity) { | ||
this.wareHouse = wareHouse; | ||
this.quantity = quantity; | ||
} | ||
|
||
public String getWareHouse() { | ||
return wareHouse; | ||
} | ||
|
||
public void setWareHouse(String wareHouse) { | ||
this.wareHouse = wareHouse; | ||
} | ||
|
||
public Integer getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(Integer quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
InStock inStock = (InStock) o; | ||
return Objects.equals(wareHouse, inStock.wareHouse) && Objects.equals(quantity, inStock.quantity); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(wareHouse, quantity); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/Inventory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.baeldung.projection.model; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import org.springframework.data.mongodb.core.mapping.MongoId; | ||
|
||
@Document(collection = "inventory") | ||
public class Inventory { | ||
|
||
@Id | ||
private String id; | ||
private String item; | ||
private String status; | ||
private Size size; | ||
private List<InStock> inStock; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getItem() { | ||
return item; | ||
} | ||
|
||
public void setItem(String item) { | ||
this.item = item; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
public Size getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(Size size) { | ||
this.size = size; | ||
} | ||
|
||
public List<InStock> getInStock() { | ||
return inStock; | ||
} | ||
|
||
public void setInStock(List<InStock> inStock) { | ||
this.inStock = inStock; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Inventory inventory = (Inventory) o; | ||
return Objects.equals(id, inventory.id) && Objects.equals(item, inventory.item) && Objects.equals(status, inventory.status); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, item, status); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...tence-modules/spring-data-mongodb-2/src/main/java/com/baeldung/projection/model/Size.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.projection.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class Size { | ||
|
||
private Double height; | ||
private Double width; | ||
private String uom; | ||
|
||
public Size(Double height, Double width, String uom) { | ||
this.height = height; | ||
this.width = width; | ||
this.uom = uom; | ||
} | ||
|
||
public Double getHeight() { | ||
return height; | ||
} | ||
|
||
public void setHeight(Double height) { | ||
this.height = height; | ||
} | ||
|
||
public Double getWidth() { | ||
return width; | ||
} | ||
|
||
public void setWidth(Double width) { | ||
this.width = width; | ||
} | ||
|
||
public String getUom() { | ||
return uom; | ||
} | ||
|
||
public void setUom(String uom) { | ||
this.uom = uom; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Size size = (Size) o; | ||
return Objects.equals(height, size.height) && Objects.equals(width, size.width) && Objects.equals(uom, size.uom); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(height, width, uom); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...-data-mongodb-2/src/main/java/com/baeldung/projection/repository/InventoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.baeldung.projection.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.data.mongodb.repository.Query; | ||
|
||
import com.baeldung.projection.model.Inventory; | ||
|
||
public interface InventoryRepository extends MongoRepository<Inventory, String> { | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'item' : 1, 'status' : 1 }") | ||
List<Inventory> findByStatusIncludeItemAndStatusFields(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'item' : 1, 'status' : 1, '_id' : 0 }") | ||
List<Inventory> findByStatusIncludeItemAndStatusExcludeIdFields(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'status' : 0, 'inStock' : 0 }") | ||
List<Inventory> findByStatusIncludeAllButStatusAndStockFields(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'item' : 1, 'status' : 1, 'size.uom': 1 }") | ||
List<Inventory> findByStatusIncludeEmbeddedFields(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'size.uom': 0 }") | ||
List<Inventory> findByStatusExcludeEmbeddedFields(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'item' : 1, 'status' : 1, 'inStock.quantity': 1 }") | ||
List<Inventory> findByStatusIncludeEmbeddedFieldsInArray(String status); | ||
|
||
@Query(value = "{ 'status' : ?0 }", fields = "{ 'item' : 1, 'status' : 1, 'inStock': { $slice: -1 } }") | ||
List<Inventory> findByStatusIncludeEmbeddedFieldsLastElementInArray(String status); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...s/spring-data-mongodb-2/src/test/java/com/baeldung/projection/AbstractTestProjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.baeldung.projection; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import com.baeldung.projection.model.InStock; | ||
import com.baeldung.projection.model.Inventory; | ||
import com.baeldung.projection.model.Size; | ||
|
||
abstract class AbstractTestProjection { | ||
|
||
public List<Inventory> getInventories() { | ||
Inventory journal = new Inventory(); | ||
journal.setItem("journal"); | ||
journal.setStatus("A"); | ||
journal.setSize(new Size(14.0, 21.0, "cm")); | ||
journal.setInStock(Collections.singletonList(new InStock("A", 5))); | ||
|
||
Inventory notebook = new Inventory(); | ||
notebook.setItem("notebook"); | ||
notebook.setStatus("A"); | ||
notebook.setSize(new Size(8.5, 11.0, "in")); | ||
notebook.setInStock(Collections.singletonList(new InStock("C", 5))); | ||
|
||
Inventory paper = new Inventory(); | ||
paper.setItem("paper"); | ||
paper.setStatus("D"); | ||
paper.setSize(new Size(8.5, 11.0, "in")); | ||
paper.setInStock(Collections.singletonList(new InStock("A", 60))); | ||
|
||
return Arrays.asList(journal, notebook, paper); | ||
} | ||
|
||
abstract void whenIncludeFields_thenOnlyIncludedFieldsAreNotNull(); | ||
|
||
abstract void whenIncludeFieldsAndExcludeOtherFields_thenOnlyExcludedFieldsAreNull(); | ||
|
||
abstract void whenIncludeAllButExcludeSomeFields_thenOnlyExcludedFieldsAreNull(); | ||
|
||
abstract void whenIncludeEmbeddedFields_thenEmbeddedFieldsAreNotNull(); | ||
|
||
abstract void whenExcludeEmbeddedFields_thenEmbeddedFieldsAreNull(); | ||
|
||
abstract void whenIncludeEmbeddedFieldsInArray_thenEmbeddedFieldsInArrayAreNotNull(); | ||
|
||
abstract void whenIncludeEmbeddedFieldsSliceInArray_thenArrayLengthEqualToSlice(); | ||
|
||
} |
Oops, something went wrong.