Skip to content

Commit 7c6725b

Browse files
authored
Merge pull request #95 from lukaseder/master
Translate Hibernate example to jOOQ
2 parents e7824b6 + 2df648c commit 7c6725b

File tree

10 files changed

+660
-0
lines changed

10 files changed

+660
-0
lines changed

java/jooq/.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Maven
2+
# -----
3+
/target
4+
5+
# IDEA
6+
# ----
7+
.idea
8+
.shelf
9+
/*.iml
10+
/*.ipr
11+
/*.iws
12+
/buildSrc/*.iml
13+
/buildSrc/*.ipr
14+
/buildSrc/*.iws
15+
/buildSrc/out
16+
/out
17+
/subprojects/*/*.iml
18+
/subprojects/*/out

java/jooq/pom.xml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.cockroachlabs</groupId>
8+
<artifactId>jooq-example</artifactId>
9+
<version>1.0</version>
10+
<name>jOOQ Example</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<jooq.distribution>org.jooq.trial-java-8</jooq.distribution>
15+
<jooq.version>3.13.0</jooq.version>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<!-- Download and install jOOQ into your artifact repository from here: https://www.jooq.org/download -->
21+
<dependency>
22+
<groupId>${jooq.distribution}</groupId>
23+
<artifactId>jooq</artifactId>
24+
<version>${jooq.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.zaxxer</groupId>
28+
<artifactId>HikariCP</artifactId>
29+
<version>3.4.2</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.postgresql</groupId>
34+
<artifactId>postgresql</artifactId>
35+
<version>9.4.1209</version>
36+
</dependency>
37+
38+
<!-- Necessary for web application -->
39+
<dependency>
40+
<groupId>org.glassfish.jersey.core</groupId>
41+
<artifactId>jersey-server</artifactId>
42+
<version>2.25</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.glassfish.jersey.containers</groupId>
46+
<artifactId>jersey-container-netty-http</artifactId>
47+
<version>2.25</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.beust</groupId>
51+
<artifactId>jcommander</artifactId>
52+
<version>1.7</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.fasterxml.jackson.core</groupId>
56+
<artifactId>jackson-databind</artifactId>
57+
<version>2.8.5</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
<version>1.8.0-beta4</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.apache.logging.log4j</groupId>
66+
<artifactId>log4j-slf4j18-impl</artifactId>
67+
<version>2.11.2</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<version>4.12</version>
74+
<scope>test</scope>
75+
</dependency>
76+
</dependencies>
77+
78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-compiler-plugin</artifactId>
83+
<version>3.8.0</version>
84+
<configuration>
85+
<release>11</release>
86+
</configuration>
87+
</plugin>
88+
89+
<plugin>
90+
<groupId>${jooq.distribution}</groupId>
91+
<artifactId>jooq-codegen-maven</artifactId>
92+
<version>${jooq.version}</version>
93+
94+
<executions>
95+
<execution>
96+
<phase>generate-sources</phase>
97+
<goals>
98+
<goal>generate</goal>
99+
</goals>
100+
</execution>
101+
</executions>
102+
103+
<configuration>
104+
<generator>
105+
<database>
106+
<name>org.jooq.meta.extensions.ddl.DDLDatabase</name>
107+
<properties>
108+
<property>
109+
<key>scripts</key>
110+
<value>${basedir}/src/main/resources/db.sql</value>
111+
</property>
112+
</properties>
113+
</database>
114+
<generate>
115+
<pojos>true</pojos>
116+
</generate>
117+
<target>
118+
<packageName>com.cockroachlabs.example.jooq.db</packageName>
119+
</target>
120+
</generator>
121+
</configuration>
122+
123+
<dependencies>
124+
<dependency>
125+
<groupId>${jooq.distribution}</groupId>
126+
<artifactId>jooq-meta-extensions</artifactId>
127+
<version>${jooq.version}</version>
128+
</dependency>
129+
</dependencies>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.cockroachlabs;
2+
3+
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.Parameter;
5+
import com.cockroachlabs.services.CustomerService;
6+
import com.cockroachlabs.services.OrderService;
7+
import com.cockroachlabs.services.PingService;
8+
import com.cockroachlabs.services.ProductService;
9+
import com.cockroachlabs.util.Utils;
10+
import org.glassfish.jersey.netty.httpserver.NettyHttpContainerProvider;
11+
import org.glassfish.jersey.server.ResourceConfig;
12+
13+
import javax.ws.rs.core.UriBuilder;
14+
import java.net.URI;
15+
16+
public class Application {
17+
18+
@Parameter(names = "-addr", description = "the address of the database")
19+
private String dbAddr;
20+
21+
public static void main(String[] args) {
22+
Application app = new Application();
23+
new JCommander(app, args);
24+
app.run();
25+
}
26+
27+
private void run() {
28+
initJOOQ();
29+
initHTTPServer();
30+
}
31+
32+
private void initJOOQ() {
33+
Utils.init(dbAddr);
34+
}
35+
36+
private void initHTTPServer() {
37+
URI baseUri = UriBuilder.fromUri("http://localhost/").port(6543).build();
38+
ResourceConfig resourceConfig = new ResourceConfig(
39+
PingService.class,
40+
CustomerService.class,
41+
ProductService.class,
42+
OrderService.class
43+
);
44+
NettyHttpContainerProvider.createServer(baseUri, resourceConfig, true);
45+
}
46+
47+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.cockroachlabs.services;
2+
3+
import com.cockroachlabs.example.jooq.db.tables.pojos.Customers;
4+
import com.cockroachlabs.example.jooq.db.tables.records.CustomersRecord;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
import javax.ws.rs.*;
9+
import java.io.IOException;
10+
import java.util.List;
11+
12+
import static com.cockroachlabs.example.jooq.db.Tables.*;
13+
import static com.cockroachlabs.util.Utils.ctx;
14+
import static org.jooq.impl.DSL.select;
15+
16+
@Path("/customer")
17+
public class CustomerService {
18+
19+
private final ObjectMapper mapper = new ObjectMapper();
20+
21+
@GET
22+
@Produces("application/json")
23+
public String getCustomers() {
24+
try {
25+
List<Customers> customers = ctx()
26+
.selectFrom(CUSTOMERS)
27+
.fetchInto(Customers.class);
28+
29+
return mapper.writeValueAsString(customers);
30+
} catch (JsonProcessingException e) {
31+
return e.toString();
32+
}
33+
}
34+
35+
@POST
36+
@Produces("application/json")
37+
public String createCustomer(String body) {
38+
try {
39+
CustomersRecord record = ctx().newRecord(CUSTOMERS, mapper.readValue(body, Customers.class));
40+
ctx().executeInsert(record);
41+
42+
return mapper.writeValueAsString(record.into(Customers.class));
43+
} catch (IOException e) {
44+
return e.toString();
45+
}
46+
}
47+
48+
@GET
49+
@Path("/{customerID}")
50+
@Produces("application/json")
51+
public String getCustomer(@PathParam("customerID") long customerID) {
52+
try {
53+
CustomersRecord record = ctx().fetchOne(CUSTOMERS, CUSTOMERS.ID.eq(customerID));
54+
if (record == null)
55+
throw new NotFoundException();
56+
57+
return mapper.writeValueAsString(record.into(Customers.class));
58+
} catch (JsonProcessingException e) {
59+
return e.toString();
60+
}
61+
}
62+
63+
@PUT
64+
@Path("/{customerID}")
65+
@Produces("application/json")
66+
public String updateCustomer(@PathParam("customerID") long customerID, String body) {
67+
try {
68+
CustomersRecord record = ctx().newRecord(CUSTOMERS, mapper.readValue(body, Customers.class));
69+
record.setId(customerID);
70+
record.reset(CUSTOMERS.ID);
71+
record.update();
72+
73+
return mapper.writeValueAsString(record.into(Customers.class));
74+
} catch (IOException e) {
75+
return e.toString();
76+
}
77+
}
78+
79+
@DELETE
80+
@Path("/{customerID}")
81+
@Produces("text/plain")
82+
public String deleteCustomer(@PathParam("customerID") long customerID) {
83+
return ctx().transactionResult(ctx -> {
84+
ctx.dsl()
85+
.deleteFrom(ORDER_PRODUCTS)
86+
.where(ORDER_PRODUCTS.ORDER_ID.in(
87+
select(ORDERS.ID)
88+
.from(ORDERS)
89+
.where(ORDERS.CUSTOMER_ID.eq(customerID))
90+
))
91+
.execute();
92+
93+
ctx.dsl()
94+
.deleteFrom(ORDERS)
95+
.where(ORDERS.CUSTOMER_ID.eq(customerID))
96+
.execute();
97+
98+
int rowCount = ctx.dsl()
99+
.deleteFrom(CUSTOMERS)
100+
.where(CUSTOMERS.ID.eq(customerID))
101+
.execute();
102+
103+
if (rowCount == 0)
104+
throw new NotFoundException();
105+
106+
return "ok";
107+
});
108+
}
109+
110+
}

0 commit comments

Comments
 (0)