Skip to content

Commit beaff00

Browse files
committed
Refine JPA integration via ByteBuddy.
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
1 parent e22ed39 commit beaff00

17 files changed

Lines changed: 860 additions & 149 deletions

File tree

jmolecules-examples/jmolecules-spring-data-jpa/pom.xml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<version>${project.version}</version>
4141
</dependency>
4242

43+
<dependency>
44+
<groupId>${project.groupId}</groupId>
45+
<artifactId>jmolecules-jpa</artifactId>
46+
<version>${project.version}</version>
47+
</dependency>
48+
4349
<dependency>
4450
<groupId>org.projectlombok</groupId>
4551
<artifactId>lombok</artifactId>
@@ -52,20 +58,6 @@
5258
<scope>runtime</scope>
5359
</dependency>
5460

55-
<dependency>
56-
<groupId>${project.groupId}</groupId>
57-
<artifactId>jmolecules-jpa</artifactId>
58-
<version>${project.version}</version>
59-
<optional>true</optional>
60-
</dependency>
61-
62-
<dependency>
63-
<groupId>${project.groupId}</groupId>
64-
<artifactId>jmolecules-spring</artifactId>
65-
<version>${project.version}</version>
66-
<optional>true</optional>
67-
</dependency>
68-
6961
<dependency>
7062
<groupId>org.springframework.boot</groupId>
7163
<artifactId>spring-boot-starter-test</artifactId>
@@ -93,13 +85,28 @@
9385
</goals>
9486
</execution>
9587
</executions>
88+
<dependencies>
89+
<dependency>
90+
<groupId>${project.groupId}</groupId>
91+
<artifactId>jmolecules-jpa-plugin</artifactId>
92+
<version>${project.version}</version>
93+
<optional>true</optional>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>${project.groupId}</groupId>
98+
<artifactId>jmolecules-spring</artifactId>
99+
<version>${project.version}</version>
100+
<optional>true</optional>
101+
</dependency>
102+
</dependencies>
96103
<configuration>
97104
<transformations>
98105
<transformation>
99106
<plugin>org.jmolecules.spring.JMoleculesSpringPlugin</plugin>
100107
</transformation>
101108
<transformation>
102-
<plugin>org.jmolecules.jpa.JMoleculesJpaPlugin</plugin>
109+
<plugin>org.jmolecules.jpa.plugin.JMoleculesJpaPlugin</plugin>
103110
</transformation>
104111
</transformations>
105112
</configuration>
@@ -109,8 +116,8 @@
109116

110117
<repositories>
111118
<repository>
112-
<id>spring-libs-snapshot</id>
113-
<url>https://repo.spring.io/libs-snapshot</url>
119+
<id>spring-snapshots</id>
120+
<url>https://repo.spring.io/snapshot</url>
114121
</repository>
115122
</repositories>
116123

jmolecules-examples/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/customer/Customer.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
*/
1616
package org.jmolecules.examples.jpa.customer;
1717

18-
import lombok.AccessLevel;
1918
import lombok.Getter;
20-
import lombok.NoArgsConstructor;
2119
import lombok.RequiredArgsConstructor;
2220
import lombok.Value;
2321

2422
import java.util.List;
2523
import java.util.UUID;
2624

27-
import javax.persistence.OneToMany;
28-
2925
import org.jmolecules.ddd.types.AggregateRoot;
3026
import org.jmolecules.ddd.types.Association;
3127
import org.jmolecules.ddd.types.Identifier;
@@ -34,13 +30,12 @@
3430
/**
3531
* @author Oliver Drotbohm
3632
*/
37-
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
3833
@Getter
3934
public class Customer implements AggregateRoot<Customer, CustomerId> {
4035

4136
private final CustomerId id;
4237
private String firstname, lastname;
43-
private @OneToMany List<Address> addresses;
38+
private List<Address> addresses;
4439

4540
public Customer(String firstname, String lastname) {
4641

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

5247
@Value
53-
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
5448
@RequiredArgsConstructor(staticName = "of")
5549
public static class CustomerId implements Identifier {
5650
private final UUID id;
5751
}
5852

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

6356
private CustomerId id;
@@ -66,5 +59,4 @@ public CustomerAssociation(Customer customer) {
6659
this.id = customer.getId();
6760
}
6861
}
69-
7062
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jmolecules.examples.jpa.customer;
17+
18+
import java.time.LocalDate;
19+
20+
import javax.persistence.GeneratedValue;
21+
import javax.persistence.Id;
22+
23+
import org.jmolecules.ddd.types.Entity;
24+
25+
/**
26+
* @author Oliver Drotbohm
27+
*/
28+
public class SomeEntity implements Entity<Customer, Long> {
29+
30+
private @Id @GeneratedValue Long id;
31+
private final LocalDate date;
32+
private String firstname;
33+
34+
public SomeEntity(String foo) {
35+
this.firstname = foo;
36+
this.date = null;
37+
}
38+
39+
/*
40+
* (non-Javadoc)
41+
* @see org.jmolecules.ddd.types.Identifiable#getId()
42+
*/
43+
@Override
44+
public Long getId() {
45+
return id;
46+
}
47+
}

jmolecules-examples/jmolecules-spring-data-jpa/src/main/java/org/jmolecules/examples/jpa/order/Order.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@
1515
*/
1616
package org.jmolecules.examples.jpa.order;
1717

18-
import lombok.AccessLevel;
1918
import lombok.EqualsAndHashCode;
2019
import lombok.Getter;
21-
import lombok.NoArgsConstructor;
2220
import lombok.RequiredArgsConstructor;
2321
import lombok.Value;
2422

2523
import java.util.List;
2624
import java.util.UUID;
2725

28-
import javax.persistence.OneToMany;
2926
import javax.persistence.Table;
3027

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

4642
private final @EqualsAndHashCode.Include OrderId id;
47-
private @OneToMany List<LineItem> lineItems;
43+
private List<LineItem> lineItems;
4844
private CustomerAssociation customer;
4945

5046
public Order(Customer customer) {
@@ -55,7 +51,6 @@ public Order(Customer customer) {
5551

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

6156
private final UUID orderId;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jmolecules.examples.jpa;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import lombok.RequiredArgsConstructor;
21+
22+
import javax.persistence.EntityManager;
23+
24+
import org.jmolecules.examples.jpa.customer.SomeEntity;
25+
import org.junit.jupiter.api.Test;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.test.context.TestConstructor;
28+
import org.springframework.test.context.TestConstructor.AutowireMode;
29+
import org.springframework.transaction.annotation.Transactional;
30+
31+
/**
32+
* @author Oliver Drotbohm
33+
*/
34+
@Transactional
35+
@SpringBootTest
36+
@TestConstructor(autowireMode = AutowireMode.ALL)
37+
@RequiredArgsConstructor
38+
class JpaTests {
39+
40+
private final EntityManager em;
41+
42+
@Test
43+
void testName() throws Exception {
44+
45+
SomeEntity entity = new SomeEntity("Test");
46+
47+
em.persist(entity);
48+
em.flush();
49+
em.clear();
50+
51+
SomeEntity result = em.find(SomeEntity.class, entity.getId());
52+
53+
assertThat(result).isNotNull();
54+
}
55+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.jmolecules</groupId>
8+
<artifactId>jmolecules-integration</artifactId>
9+
<version>1.1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<name>jMolecules - JPA ByteBuddy plugin</name>
13+
<artifactId>jmolecules-jpa-plugin</artifactId>
14+
15+
<dependencies>
16+
17+
<dependency>
18+
<groupId>${project.groupId}</groupId>
19+
<artifactId>jmolecules-ddd</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>${project.groupId}</groupId>
25+
<artifactId>jmolecules-jpa</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.data</groupId>
31+
<artifactId>spring-data-commons</artifactId>
32+
<version>2.4.2</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>net.bytebuddy</groupId>
37+
<artifactId>byte-buddy</artifactId>
38+
<version>${bytebuddy.version}</version>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>net.bytebuddy</groupId>
47+
<artifactId>byte-buddy-maven-plugin</artifactId>
48+
<version>${bytebuddy.version}</version>
49+
<executions>
50+
<execution>
51+
<goals>
52+
<goal>transform-test</goal>
53+
</goals>
54+
</execution>
55+
</executions>
56+
<dependencies>
57+
<dependency>
58+
<groupId>${project.groupId}</groupId>
59+
<artifactId>${project.artifactId}</artifactId>
60+
<version>${project.version}</version>
61+
</dependency>
62+
</dependencies>
63+
<configuration>
64+
<transformations>
65+
<transformation>
66+
<plugin>org.jmolecules.jpa.plugin.JMoleculesJpaPlugin</plugin>
67+
</transformation>
68+
</transformations>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>

0 commit comments

Comments
 (0)