Skip to content

Commit

Permalink
Refine JPA integration via ByteBuddy.
Browse files Browse the repository at this point in the history
Significant rework in the JPA integration via a ByteBuddy plugin. All features supported now can be found in the readme of the jmolecules-integration/jmolecules-jpa module.

Separated ByteBuddy plugin from JPA integration code needed at runtime.

Related discussions: #44
  • Loading branch information
odrotbohm committed Jan 11, 2021
1 parent e22ed39 commit beaff00
Show file tree
Hide file tree
Showing 17 changed files with 860 additions and 149 deletions.
41 changes: 24 additions & 17 deletions jmolecules-examples/jmolecules-spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-jpa</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -52,20 +58,6 @@
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-jpa</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-spring</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -93,13 +85,28 @@
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-jpa-plugin</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-spring</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
<configuration>
<transformations>
<transformation>
<plugin>org.jmolecules.spring.JMoleculesSpringPlugin</plugin>
</transformation>
<transformation>
<plugin>org.jmolecules.jpa.JMoleculesJpaPlugin</plugin>
<plugin>org.jmolecules.jpa.plugin.JMoleculesJpaPlugin</plugin>
</transformation>
</transformations>
</configuration>
Expand All @@ -109,8 +116,8 @@

<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</repository>
</repositories>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
*/
package org.jmolecules.examples.jpa.customer;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Value;

import java.util.List;
import java.util.UUID;

import javax.persistence.OneToMany;

import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Association;
import org.jmolecules.ddd.types.Identifier;
Expand All @@ -34,13 +30,12 @@
/**
* @author Oliver Drotbohm
*/
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@Getter
public class Customer implements AggregateRoot<Customer, CustomerId> {

private final CustomerId id;
private String firstname, lastname;
private @OneToMany List<Address> addresses;
private List<Address> addresses;

public Customer(String firstname, String lastname) {

Expand All @@ -50,14 +45,12 @@ public Customer(String firstname, String lastname) {
}

@Value
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@RequiredArgsConstructor(staticName = "of")
public static class CustomerId implements Identifier {
private final UUID id;
}

@Getter
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public static class CustomerAssociation implements Association<Customer, CustomerId> {

private CustomerId id;
Expand All @@ -66,5 +59,4 @@ public CustomerAssociation(Customer customer) {
this.id = customer.getId();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 org.jmolecules.examples.jpa.customer;

import java.time.LocalDate;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.jmolecules.ddd.types.Entity;

/**
* @author Oliver Drotbohm
*/
public class SomeEntity implements Entity<Customer, Long> {

private @Id @GeneratedValue Long id;
private final LocalDate date;
private String firstname;

public SomeEntity(String foo) {
this.firstname = foo;
this.date = null;
}

/*
* (non-Javadoc)
* @see org.jmolecules.ddd.types.Identifiable#getId()
*/
@Override
public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*/
package org.jmolecules.examples.jpa.order;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Value;

import java.util.List;
import java.util.UUID;

import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.jmolecules.ddd.types.AggregateRoot;
Expand All @@ -39,12 +36,11 @@
@Table(name = "SAMPLE_ORDER")
@Getter
@RequiredArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Order implements AggregateRoot<Order, Order.OrderId> {

private final @EqualsAndHashCode.Include OrderId id;
private @OneToMany List<LineItem> lineItems;
private List<LineItem> lineItems;
private CustomerAssociation customer;

public Order(Customer customer) {
Expand All @@ -55,7 +51,6 @@ public Order(Customer customer) {

@Value
@RequiredArgsConstructor(staticName = "of")
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public static class OrderId implements Identifier {

private final UUID orderId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 org.jmolecules.examples.jpa;

import static org.assertj.core.api.Assertions.*;

import lombok.RequiredArgsConstructor;

import javax.persistence.EntityManager;

import org.jmolecules.examples.jpa.customer.SomeEntity;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestConstructor.AutowireMode;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Oliver Drotbohm
*/
@Transactional
@SpringBootTest
@TestConstructor(autowireMode = AutowireMode.ALL)
@RequiredArgsConstructor
class JpaTests {

private final EntityManager em;

@Test
void testName() throws Exception {

SomeEntity entity = new SomeEntity("Test");

em.persist(entity);
em.flush();
em.clear();

SomeEntity result = em.find(SomeEntity.class, entity.getId());

assertThat(result).isNotNull();
}
}
73 changes: 73 additions & 0 deletions jmolecules-integration/jmolecules-jpa-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<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.jmolecules</groupId>
<artifactId>jmolecules-integration</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<name>jMolecules - JPA ByteBuddy plugin</name>
<artifactId>jmolecules-jpa-plugin</artifactId>

<dependencies>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-ddd</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jmolecules-jpa</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.4.2</version>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${bytebuddy.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<version>${bytebuddy.version}</version>
<executions>
<execution>
<goals>
<goal>transform-test</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<configuration>
<transformations>
<transformation>
<plugin>org.jmolecules.jpa.plugin.JMoleculesJpaPlugin</plugin>
</transformation>
</transformations>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit beaff00

Please sign in to comment.