Add Spring Data JDBC example for jMolecules.
This commit is contained in:
committed by
Oliver Drotbohm
parent
5197c10e62
commit
e2b9de4fbb
69
jdbc/jmolecules/pom.xml
Normal file
69
jdbc/jmolecules/pom.xml
Normal file
@@ -0,0 +1,69 @@
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-jdbc-examples</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>Spring Data JDBC - jMolecules Example</name>
|
||||
<artifactId>spring-data-jdbc-jmolecules</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring-data-bom.version>2021.0.0-SNAPSHOT</spring-data-bom.version>
|
||||
<jmolecules.version>1.2.0</jmolecules.version>
|
||||
<jmolecules-integration.version>0.2.0</jmolecules-integration.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- jMolecules -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jmolecules.integrations</groupId>
|
||||
<artifactId>jmolecules-spring</artifactId>
|
||||
<version>${jmolecules-integration.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jmolecules</groupId>
|
||||
<artifactId>jmolecules-events</artifactId>
|
||||
<version>${jmolecules.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-maven-plugin</artifactId>
|
||||
<version>${byte-buddy.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>transform</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jmolecules.integrations</groupId>
|
||||
<artifactId>jmolecules-bytebuddy</artifactId>
|
||||
<version>${jmolecules-integration.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
28
jdbc/jmolecules/src/main/resources/schema.sql
Normal file
28
jdbc/jmolecules/src/main/resources/schema.sql
Normal 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);
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<modules>
|
||||
<module>basics</module>
|
||||
<module>mybatis</module>
|
||||
<module>jmolecules</module>
|
||||
<module>jooq</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user