Add Spring Data JDBC example for jMolecules.

This commit is contained in:
Oliver Drotbohm
2021-03-16 18:04:41 +01:00
committed by Oliver Drotbohm
parent 5197c10e62
commit e2b9de4fbb
11 changed files with 447 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Oliver Drotbohm
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.customer;
import lombok.Value;
import org.jmolecules.ddd.annotation.ValueObject;
@Value
@ValueObject
public class Address {
private final String street, city, zipCode;
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.customer;
import example.springdata.jdbc.jmolecules.customer.Customer.CustomerId;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.Value;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Identifier;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.util.Assert;
/**
* @author Oliver Drotbohm
*/
@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Customer implements AggregateRoot<Customer, CustomerId> {
private final CustomerId id;
private @Setter String firstname, lastname;
private List<Address> addresses;
public Customer(String firstname, String lastname, Address address) {
Assert.notNull(address, "Address must not be null!");
this.id = CustomerId.of(UUID.randomUUID().toString());
this.firstname = firstname;
this.lastname = lastname;
this.addresses = new ArrayList<>();
this.addresses.add(address);
}
@Value(staticConstructor = "of")
public static class CustomerId implements Identifier {
private final String id;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.customer;
import example.springdata.jdbc.jmolecules.customer.Customer.CustomerId;
import org.jmolecules.ddd.types.Repository;
import org.jmolecules.spring.AssociationResolver;
/**
* @author Oliver Drotbohm
*/
public interface Customers extends Repository<Customer, CustomerId>, AssociationResolver<Customer, CustomerId> {
Customer save(Customer customer);
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.order;
import lombok.Value;
import org.jmolecules.ddd.annotation.ValueObject;
@Value
@ValueObject
public class LineItem {
String description;
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.order;
import example.springdata.jdbc.jmolecules.customer.Customer;
import example.springdata.jdbc.jmolecules.customer.Customer.CustomerId;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.Value;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
/**
* @author Oliver Drotbohm
*/
@Table("MY_ORDER")
@Getter
@ToString
@RequiredArgsConstructor
public class Order implements AggregateRoot<Order, Order.OrderId> {
private final OrderId id;
private @Column("FOO") List<LineItem> lineItems;
private Association<Customer, CustomerId> customer;
public Order(Customer customer) {
this.id = OrderId.of(UUID.randomUUID());
this.customer = Association.forAggregate(customer);
this.lineItems = new ArrayList<>();
}
public Order addLineItem(String description) {
LineItem item = new LineItem(description);
this.lineItems.add(item);
return this;
}
@Value(staticConstructor = "of")
public static class OrderId implements Identifier {
private final UUID orderId;
public static OrderId create() {
return OrderId.of(UUID.randomUUID());
}
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules.order;
import example.springdata.jdbc.jmolecules.order.Order.OrderId;
import org.jmolecules.ddd.types.Repository;
import org.jmolecules.spring.AssociationResolver;
/**
* @author Oliver Drotbohm
*/
public interface Orders extends Repository<Order, OrderId>, AssociationResolver<Order, OrderId> {
Order save(Order order);
}

View File

@@ -0,0 +1,28 @@
CREATE TABLE IF NOT EXISTS customer (
id VARCHAR(100) PRIMARY KEY,
firstname VARCHAR(100),
lastname VARCHAR(100)
);
CREATE TABLE IF NOT EXISTS address (
street VARCHAR(100),
city VARCHAR(100),
zip_code VARCHAR(100),
customer VARCHAR(100),
customer_key VARCHAR(100)
);
CREATE TABLE IF NOT EXISTS my_order (
id VARCHAR(100),
customer VARCHAR(100)
);
CREATE TABLE IF NOT EXISTS line_item (
my_order_key VARCHAR(100),
description VARCHAR(100),
foo VARCHAR(100)
);
// CREATE TABLE IF NOT EXISTS Lego_Set (id INTEGER, name VARCHAR(100), min_Age INTEGER, max_Age INTEGER);
// CREATE TABLE IF NOT EXISTS Handbuch (handbuch_id INTEGER, author VARCHAR(100), text CLOB);
// CREATE TABLE IF NOT EXISTS Model (name VARCHAR(100), description CLOB, lego_set INTEGER);

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.jmolecules;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jdbc.jmolecules.customer.Address;
import example.springdata.jdbc.jmolecules.customer.Customer;
import example.springdata.jdbc.jmolecules.customer.Customers;
import example.springdata.jdbc.jmolecules.order.Order;
import example.springdata.jdbc.jmolecules.order.Orders;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
/**
* @author Oliver Drotbohm
*/
@SpringBootTest
@RequiredArgsConstructor
class ApplicationIntegrationTests {
private final ConfigurableApplicationContext context;
@Test
void exposesAssociationInMetamodel() {
JdbcMappingContext mapping = context.getBean(JdbcMappingContext.class);
RelationalPersistentEntity<?> entity = mapping.getRequiredPersistentEntity(Order.class);
RelationalPersistentProperty customer = entity.getRequiredPersistentProperty("customer");
assertThat(customer.isAssociation()).isTrue();
}
@Test
void persistsDomainModel() {
Address address = new Address("41 Greystreet", "Dreaming Tree", "2731");
Customers customers = context.getBean(Customers.class);
Customer customer = customers.save(new Customer("Dave", "Matthews", address));
customer.setFirstname("Carter");
customer = customers.save(customer);
Orders orders = context.getBean(Orders.class);
Order order = new Order(customer)
.addLineItem("Foo")
.addLineItem("Bar");
Order result = orders.save(order);
assertThat(customers.resolveRequired(result.getCustomer()));
}
}