Skip to content

Commit e1056e0

Browse files
mikewojtynaecheyne
authored andcommitted
Add BAEL-2272 persisting DDD aggregates examples (eugenp#5630)
* Add BAEL-2272 persisting DDD aggregates examples * Update pom.xml
1 parent 91ec114 commit e1056e0

15 files changed

+746
-0
lines changed

ddd/pom.xml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.6.RELEASE</version>
9+
<relativePath /> <!-- lookup parent from repository -->
10+
</parent>
11+
12+
<groupId>com.baeldung.ddd</groupId>
13+
<artifactId>ddd</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
<name>ddd</name>
17+
<description>DDD series examples</description>
18+
19+
<properties>
20+
<joda-money.version>1.0.1</joda-money.version>
21+
<maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-engine</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<!-- JUnit platform launcher -->
40+
<!-- To be able to run tests from IDE directly -->
41+
<dependency>
42+
<groupId>org.junit.platform</groupId>
43+
<artifactId>junit-platform-launcher</artifactId>
44+
<version>${junit-platform.version}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.joda</groupId>
49+
<artifactId>joda-money</artifactId>
50+
<version>${joda-money.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-data-jpa</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.h2database</groupId>
58+
<artifactId>h2</artifactId>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>mysql</groupId>
63+
<artifactId>mysql-connector-java</artifactId>
64+
<scope>runtime</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.postgresql</groupId>
68+
<artifactId>postgresql</artifactId>
69+
<scope>runtime</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-devtools</artifactId>
74+
<optional>true</optional>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-starter-web</artifactId>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.springframework.boot</groupId>
82+
<artifactId>spring-boot-starter-test</artifactId>
83+
<scope>test</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>de.flapdoodle.embed</groupId>
87+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
88+
<scope>test</scope>
89+
</dependency>
90+
</dependencies>
91+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.ddd;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class PersistingDddAggregatesApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(PersistingDddAggregatesApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.ddd.order;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.joda.money.Money;
7+
8+
public class Order {
9+
private final List<OrderLine> orderLines;
10+
private Money totalCost;
11+
12+
public Order(List<OrderLine> orderLines) {
13+
checkNotNull(orderLines);
14+
if (orderLines.isEmpty()) {
15+
throw new IllegalArgumentException("Order must have at least one order line item");
16+
}
17+
this.orderLines = new ArrayList<>(orderLines);
18+
totalCost = calculateTotalCost();
19+
}
20+
21+
public void addLineItem(OrderLine orderLine) {
22+
checkNotNull(orderLine);
23+
orderLines.add(orderLine);
24+
totalCost = totalCost.plus(orderLine.cost());
25+
}
26+
27+
public List<OrderLine> getOrderLines() {
28+
return new ArrayList<>(orderLines);
29+
}
30+
31+
public void removeLineItem(int line) {
32+
OrderLine removedLine = orderLines.remove(line);
33+
totalCost = totalCost.minus(removedLine.cost());
34+
}
35+
36+
public Money totalCost() {
37+
return totalCost;
38+
}
39+
40+
private Money calculateTotalCost() {
41+
return orderLines.stream()
42+
.map(OrderLine::cost)
43+
.reduce(Money::plus)
44+
.get();
45+
}
46+
47+
private static void checkNotNull(Object par) {
48+
if (par == null) {
49+
throw new NullPointerException("Parameter cannot be null");
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.ddd.order;
2+
3+
import org.joda.money.Money;
4+
5+
public class OrderLine {
6+
private final Product product;
7+
private final int quantity;
8+
9+
public OrderLine(Product product, int quantity) {
10+
super();
11+
this.product = product;
12+
this.quantity = quantity;
13+
}
14+
15+
@Override
16+
public boolean equals(Object obj) {
17+
if (this == obj) {
18+
return true;
19+
}
20+
if (obj == null) {
21+
return false;
22+
}
23+
if (getClass() != obj.getClass()) {
24+
return false;
25+
}
26+
OrderLine other = (OrderLine) obj;
27+
if (product == null) {
28+
if (other.product != null) {
29+
return false;
30+
}
31+
} else if (!product.equals(other.product)) {
32+
return false;
33+
}
34+
if (quantity != other.quantity) {
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
final int prime = 31;
43+
int result = 1;
44+
result = prime * result + ((product == null) ? 0 : product.hashCode());
45+
result = prime * result + quantity;
46+
return result;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "OrderLine [product=" + product + ", quantity=" + quantity + "]";
52+
}
53+
54+
Money cost() {
55+
return product.getPrice()
56+
.multipliedBy(quantity);
57+
}
58+
59+
Product getProduct() {
60+
return product;
61+
}
62+
63+
int getQuantity() {
64+
return quantity;
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.ddd.order;
2+
3+
import org.joda.money.Money;
4+
5+
public class Product {
6+
private final Money price;
7+
8+
public Product(Money price) {
9+
super();
10+
this.price = price;
11+
}
12+
13+
@Override
14+
public boolean equals(Object obj) {
15+
if (this == obj) {
16+
return true;
17+
}
18+
if (obj == null) {
19+
return false;
20+
}
21+
if (getClass() != obj.getClass()) {
22+
return false;
23+
}
24+
Product other = (Product) obj;
25+
if (price == null) {
26+
if (other.price != null) {
27+
return false;
28+
}
29+
} else if (!price.equals(other.price)) {
30+
return false;
31+
}
32+
return true;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
final int prime = 31;
38+
int result = 1;
39+
result = prime * result + ((price == null) ? 0 : price.hashCode());
40+
return result;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "Product [price=" + price + "]";
46+
}
47+
48+
Money getPrice() {
49+
return price;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.baeldung.ddd.order.jpa;
2+
3+
import java.math.BigDecimal;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.persistence.ElementCollection;
8+
import javax.persistence.Entity;
9+
import javax.persistence.FetchType;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
import javax.persistence.Table;
13+
14+
@Entity
15+
@Table(name = "order_table")
16+
class JpaOrder {
17+
private String currencyUnit;
18+
@Id
19+
@GeneratedValue
20+
private Long id;
21+
@ElementCollection(fetch = FetchType.EAGER)
22+
private final List<JpaOrderLine> orderLines;
23+
private BigDecimal totalCost;
24+
25+
JpaOrder() {
26+
totalCost = null;
27+
orderLines = new ArrayList<>();
28+
}
29+
30+
JpaOrder(List<JpaOrderLine> orderLines) {
31+
checkNotNull(orderLines);
32+
if (orderLines.isEmpty()) {
33+
throw new IllegalArgumentException("Order must have at least one order line item");
34+
}
35+
this.orderLines = new ArrayList<>(orderLines);
36+
}
37+
38+
@Override
39+
public boolean equals(Object obj) {
40+
if (this == obj) {
41+
return true;
42+
}
43+
if (obj == null) {
44+
return false;
45+
}
46+
if (getClass() != obj.getClass()) {
47+
return false;
48+
}
49+
JpaOrder other = (JpaOrder) obj;
50+
if (id == null) {
51+
if (other.id != null) {
52+
return false;
53+
}
54+
} else if (!id.equals(other.id)) {
55+
return false;
56+
}
57+
return true;
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
final int prime = 31;
63+
int result = 1;
64+
result = prime * result + ((id == null) ? 0 : id.hashCode());
65+
return result;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "JpaOrder [currencyUnit=" + currencyUnit + ", id=" + id + ", orderLines=" + orderLines + ", totalCost=" + totalCost + "]";
71+
}
72+
73+
void addLineItem(JpaOrderLine orderLine) {
74+
checkNotNull(orderLine);
75+
orderLines.add(orderLine);
76+
}
77+
78+
String getCurrencyUnit() {
79+
return currencyUnit;
80+
}
81+
82+
Long getId() {
83+
return id;
84+
}
85+
86+
List<JpaOrderLine> getOrderLines() {
87+
return new ArrayList<>(orderLines);
88+
}
89+
90+
BigDecimal getTotalCost() {
91+
return totalCost;
92+
}
93+
94+
void removeLineItem(int line) {
95+
JpaOrderLine removedLine = orderLines.remove(line);
96+
}
97+
98+
void setCurrencyUnit(String currencyUnit) {
99+
this.currencyUnit = currencyUnit;
100+
}
101+
102+
void setTotalCost(BigDecimal totalCost) {
103+
this.totalCost = totalCost;
104+
}
105+
106+
private static void checkNotNull(Object par) {
107+
if (par == null) {
108+
throw new NullPointerException("Parameter cannot be null");
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)