Skip to content

Commit 4e9fdae

Browse files
committed
Bumped spring boot version
Added roach-data-jpa-orders demo
1 parent 2044892 commit 4e9fdae

File tree

25 files changed

+1170
-27
lines changed

25 files changed

+1170
-27
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Roach Demo Data
1+
# Roach Data
22

33
Collection of small Spring Boot demos using CockroachDB with common data access frameworks and ORMs.
44
The purpose is to showcase how CockroachDB can be used with a mainstream Enterprise Java framework
@@ -9,6 +9,7 @@ Data access variants include:
99
- [JDBC](roach-data-jdbc/README.md) - using Spring Data JDBC
1010
- [JDBC (plain)](roach-data-jdbc-plain/README.md) - using plain JDBC
1111
- [JPA](roach-data-jpa/README.md) - using Spring Data JPA with Hibernate as ORM provider
12+
- [JPA Orders](roach-data-jpa-orders/README.md) - using Spring Data JPA to model a simple order system
1213
- [jOOQ](roach-data-jooq/README.md) - using Spring Boot with jOOQ (not officially supported by spring-data)
1314
- [MyBatis](roach-data-mybatis/README.md) - using Spring Data MyBatis/JDBC
1415
- [JSON](roach-data-json/README.md) - using Spring Data JPA and JSONB types with inverted indexes
@@ -58,23 +59,25 @@ The service remains running after the test is complete and can be access via:
5859
- http://localhost:9090
5960

6061
You could use something like Postman to send requests to the API on your own.
61-
62-
A custom database URL is specified with a config override:
63-
64-
--spring.datasource.url=jdbc:postgresql://192.168.1.123:26257/roach_data?sslmode=disable
6562

6663
### JDBC demo
6764

68-
java -jar roach-data-jdbc/target/roach-data-jdbc.jar
65+
(Using custom JDBC URL)
66+
67+
java -jar roach-data-jdbc/target/roach-data-jdbc.jar --spring.datasource.url=jdbc:postgresql://localhost:26257/roach_data?sslmode=disable
6968

70-
with contention:
69+
Run with contention/retries:
7170

72-
java -jar roach-data-jdbc/target/roach-data-jdbc.jar 8
71+
java -jar roach-data-jdbc/target/roach-data-jdbc.jar --concurrency=8
7372

7473
### JPA demo
7574

7675
java -jar roach-data-jpa/target/roach-data-jpa.jar
7776

77+
### JPA orders demo
78+
79+
java -jar roach-data-jpa-orders/target/roach-data-jpa-orders.jar
80+
7881
### jOOQ demo
7982

8083
java -jar roach-data-jooq/target/roach-data-jooq.jar

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.4.0</version>
8+
<version>2.5.6</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111

@@ -24,6 +24,7 @@
2424
<module>roach-data-jdbc-plain</module>
2525
<module>roach-data-jooq</module>
2626
<module>roach-data-jpa</module>
27+
<module>roach-data-jpa-orders</module>
2728
<module>roach-data-json</module>
2829
<module>roach-data-mybatis</module>
2930
</modules>
@@ -33,7 +34,7 @@
3334
<dependency>
3435
<groupId>org.springframework.boot</groupId>
3536
<artifactId>spring-boot-starter-web</artifactId>
36-
<version>2.4.0</version>
37+
<version>2.5.6</version>
3738
<exclusions>
3839
<!-- Exclude the Tomcat dependency -->
3940
<exclusion>

roach-data-jdbc/src/main/java/io/roach/data/jdbc/JdbcApplication.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java.math.BigDecimal;
44
import java.util.ArrayDeque;
5+
import java.util.Arrays;
56
import java.util.Deque;
67
import java.util.HashMap;
8+
import java.util.LinkedList;
79
import java.util.Map;
810
import java.util.concurrent.ExecutionException;
911
import java.util.concurrent.Executors;
@@ -43,12 +45,20 @@ public static void main(String[] args) {
4345
}
4446

4547
@Override
46-
public void run(String... args) throws Exception {
47-
logger.info("Lets move some $$ around!");
48-
48+
public void run(String... args) {
4949
final Link transferLink = Link.of("http://localhost:9090/transfer/{?fromId,toId,amount}");
5050

51-
final int concurrency = args.length > 0 ? Integer.parseInt(args[0]) : 1;
51+
int concurrency = 1;
52+
53+
LinkedList<String> argsList = new LinkedList<>(Arrays.asList(args));
54+
while (!argsList.isEmpty()) {
55+
String arg = argsList.pop();
56+
if (arg.startsWith("--concurrency=")) {
57+
concurrency = Integer.parseInt(arg.split("=")[1]);
58+
}
59+
}
60+
61+
logger.info("Lets move some $$ around! (concurrency level {})", concurrency);
5262

5363
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(concurrency);
5464

roach-data-jdbc/src/main/resources/db/create.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ create table account
77
updated timestamptz not null default clock_timestamp()
88
);
99

10-
insert into account (id,balance,name,type)
11-
values
12-
(1,100.50,'a','expense'),
13-
(2,100.50,'b','expense'),
14-
(3,100.50,'c','expense'),
15-
(4,100.50,'d','expense'),
16-
(5,100.50,'e','expense');
10+
-- insert into account (id,balance,name,type)
11+
-- values
12+
-- (1,100.50,'a','expense'),
13+
-- (2,100.50,'b','expense'),
14+
-- (3,100.50,'c','expense'),
15+
-- (4,100.50,'d','expense'),
16+
-- (5,100.50,'e','expense');
1717

18-
select * from account AS OF SYSTEM TIME '-5s';
18+
-- select * from account AS OF SYSTEM TIME '-5s';

roach-data-jpa-orders/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Roach Demo Data :: JPA/Hibernate Orders
2+
3+
A CockroachDB Spring Boot Demo using [Spring Data JPA](https://spring.io/projects/spring-data-jpa)
4+
with Hibernate for data access.
5+
6+
7+

roach-data-jpa-orders/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>io.roach.data</groupId>
7+
<artifactId>roach-data-parent</artifactId>
8+
<version>1.0.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>roach-data-jpa-orders</artifactId>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-web</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-jetty</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-hateoas</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-jdbc</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-data-jpa</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.data</groupId>
40+
<artifactId>spring-data-commons</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.hibernate</groupId>
45+
<artifactId>hibernate-core</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.flywaydb</groupId>
49+
<artifactId>flyway-core</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>net.ttddyy</groupId>
54+
<artifactId>datasource-proxy</artifactId>
55+
<version>1.7</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.postgresql</groupId>
59+
<artifactId>postgresql</artifactId>
60+
<version>42.2.24</version>
61+
<scope>runtime</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<finalName>roach-data-jpa-orders</finalName>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.roach.data.jpa;
2+
3+
import org.springframework.boot.WebApplicationType;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
7+
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
8+
import org.springframework.boot.builder.SpringApplicationBuilder;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
12+
13+
@Configuration
14+
@EnableAutoConfiguration(exclude = {
15+
TransactionAutoConfiguration.class,
16+
DataSourceTransactionManagerAutoConfiguration.class,
17+
DataSourceAutoConfiguration.class
18+
})
19+
@EnableAspectJAutoProxy(proxyTargetClass = true)
20+
@ComponentScan(basePackages = "io.roach")
21+
public class JpaOrdersApplication {
22+
public static void main(String[] args) {
23+
new SpringApplicationBuilder(JpaOrdersApplication.class)
24+
.web(WebApplicationType.NONE)
25+
.run(args);
26+
}
27+
}
28+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.roach.data.jpa;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.Arrays;
8+
import java.util.EnumSet;
9+
10+
import org.hibernate.boot.Metadata;
11+
import org.hibernate.boot.MetadataBuilder;
12+
import org.hibernate.boot.MetadataSources;
13+
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
14+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
15+
import org.hibernate.dialect.CockroachDB201Dialect;
16+
import org.hibernate.dialect.PostgreSQL10Dialect;
17+
import org.hibernate.service.ServiceRegistry;
18+
import org.hibernate.tool.hbm2ddl.SchemaExport;
19+
import org.hibernate.tool.schema.TargetType;
20+
21+
import io.roach.data.jpa.domain.Customer;
22+
import io.roach.data.jpa.domain.Order;
23+
import io.roach.data.jpa.domain.Product;
24+
25+
/**
26+
* Hibernate DDL exporter from JPA annotations for verification of mapping.
27+
*/
28+
public class SchemaExporter {
29+
public static final Class[] ENTITY_TYPES = {
30+
Customer.class,
31+
Order.class,
32+
Product.class
33+
};
34+
35+
public static void _main(String[] args) throws IOException {
36+
boolean drop = true;
37+
Path outFile = null;
38+
String delimiter = ";";
39+
Class dialect = CockroachDB201Dialect.class;
40+
41+
for (String arg : args) {
42+
if (arg.startsWith("--")) {
43+
if ("--drop".equals(arg)) {
44+
drop = true;
45+
} else if (arg.startsWith("--output=")) {
46+
outFile = Paths.get(arg.substring(9));
47+
} else if (arg.startsWith("--delimiter=")) {
48+
delimiter = arg.substring(12);
49+
} else if (arg.equalsIgnoreCase("--psql")) {
50+
dialect = PostgreSQL10Dialect.class;
51+
}
52+
} else {
53+
try {
54+
dialect = Class.forName(arg);
55+
} catch (ClassNotFoundException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
}
60+
61+
if (outFile == null) {
62+
outFile = Files.createTempFile("shop_n_drop", "schema");
63+
}
64+
65+
ServiceRegistry standardRegistry =
66+
new StandardServiceRegistryBuilder()
67+
.applySetting("hibernate.dialect", dialect.getName())
68+
.applySetting("hibernate.physical_naming_strategy",
69+
"org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy")
70+
.build();
71+
72+
MetadataSources metadataSources = new MetadataSources(standardRegistry);
73+
74+
Arrays.stream(ENTITY_TYPES).sequential().forEach(clazz -> metadataSources.addAnnotatedClass(clazz));
75+
76+
MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
77+
metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE);
78+
Metadata metadata = metadataBuilder.build();
79+
80+
// Exporter doesnt seem to export if file exists
81+
if (outFile.toFile().isFile() && !outFile.toFile().delete()) {
82+
System.err.println("WARN: Unable to delete file " + outFile);
83+
}
84+
85+
SchemaExport schemaExport = new SchemaExport()
86+
.setOutputFile(outFile.toString())
87+
.setFormat(true)
88+
.setHaltOnError(true)
89+
.setDelimiter(delimiter);
90+
if (drop) {
91+
schemaExport.create(EnumSet.of(TargetType.SCRIPT), metadata);
92+
} else {
93+
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata);
94+
}
95+
96+
Files.copy(outFile, System.out);
97+
}
98+
99+
private SchemaExporter() {
100+
}
101+
}

0 commit comments

Comments
 (0)