Skip to content

Commit 91f908b

Browse files
committed
Added roach-data-jpa-json subproject to showcase JSONB mappings.
Bumped spring boot version.
1 parent fa3e583 commit 91f908b

38 files changed

+1938
-7
lines changed

pom.xml

Lines changed: 3 additions & 7 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.2.7.RELEASE</version>
8+
<version>2.3.4.RELEASE</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111

@@ -23,6 +23,7 @@
2323
<module>roach-data-jooq</module>
2424
<module>roach-data-jdbc</module>
2525
<module>roach-data-jpa</module>
26+
<module>roach-data-jpa-json</module>
2627
<module>roach-data-mybatis</module>
2728
</modules>
2829

@@ -31,7 +32,7 @@
3132
<dependency>
3233
<groupId>org.springframework.boot</groupId>
3334
<artifactId>spring-boot-starter-web</artifactId>
34-
<version>2.2.7.RELEASE</version>
35+
<version>2.3.4.RELEASE</version>
3536
<exclusions>
3637
<!-- Exclude the Tomcat dependency -->
3738
<exclusion>
@@ -46,11 +47,6 @@
4647
<version>3.8.9</version>
4748
<scope>runtime</scope>
4849
</dependency>
49-
<dependency>
50-
<groupId>org.springframework.data</groupId>
51-
<artifactId>spring-data-commons</artifactId>
52-
<version>2.2.7.RELEASE</version>
53-
</dependency>
5450
</dependencies>
5551
</dependencyManagement>
5652

roach-data-jpa-json/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Roach Demo Data :: JPA/Hibernate with JSONB
2+
3+
A CockroachDB Spring Boot Demo using [Spring Data JPA](https://spring.io/projects/spring-data-jpa)
4+
with Hibernate for data access. Demonstrates various JSONB column type mappings with inverted
5+
and computed indexes.

roach-data-jpa-json/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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-json</artifactId>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-jdbc</artifactId>
17+
</dependency>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-data-jpa</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.liquibase</groupId>
24+
<artifactId>liquibase-core</artifactId>
25+
<scope>runtime</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.data</groupId>
29+
<artifactId>spring-data-commons</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.fasterxml.jackson.core</groupId>
33+
<artifactId>jackson-databind</artifactId>
34+
<!-- <version>2.11.2</version>-->
35+
</dependency>
36+
<dependency>
37+
<groupId>org.postgresql</groupId>
38+
<artifactId>postgresql</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
<!-- ======================================================== -->
43+
<!-- Test -->
44+
<!-- ======================================================== -->
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-engine</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
<build>
59+
<finalName>roach-data-jpa</finalName>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.roach.data.jpa.chat;
2+
3+
import java.util.List;
4+
import java.util.UUID;
5+
6+
import javax.persistence.*;
7+
8+
import org.hibernate.annotations.Type;
9+
import org.hibernate.annotations.TypeDef;
10+
11+
import io.roach.data.jpa.support.AbstractJsonDataType;
12+
13+
@Entity
14+
@Table(name = "chat_history")
15+
@TypeDef(name = "jsonb-message", typeClass = ChatHistory.StringCollectionJsonType.class)
16+
public class ChatHistory {
17+
public static class StringCollectionJsonType extends AbstractJsonDataType<String> {
18+
@Override
19+
public Class<String> returnedClass() {
20+
return String.class;
21+
}
22+
23+
@Override
24+
public boolean isCollectionType() {
25+
return true;
26+
}
27+
}
28+
29+
@Id
30+
@GeneratedValue(strategy = GenerationType.AUTO)
31+
private UUID id;
32+
33+
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
34+
@JoinColumn(name = "parent_id")
35+
private ChatHistory parent;
36+
37+
@Type(type = "jsonb-message")
38+
@Column(name = "messages")
39+
@Basic(fetch = FetchType.LAZY)
40+
private List<String> messages;
41+
42+
public UUID getId() {
43+
return id;
44+
}
45+
46+
public ChatHistory getParent() {
47+
return parent;
48+
}
49+
50+
public void setParent(ChatHistory parent) {
51+
this.parent = parent;
52+
}
53+
54+
public List<String> getMessages() {
55+
return messages;
56+
}
57+
58+
public void setMessages(List<String> messages) {
59+
this.messages = messages;
60+
}
61+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.roach.data.jpa.chat;
2+
3+
import java.util.UUID;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface ChatHistoryRepository extends JpaRepository<ChatHistory, UUID> {
10+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.roach.data.jpa.department;
2+
3+
public class Address {
4+
public static class Builder {
5+
private final Address adddress = new Address();
6+
7+
public Builder setAddress1(String address1) {
8+
adddress.address1 = address1;
9+
return this;
10+
}
11+
12+
public Builder setAddress2(String address2) {
13+
adddress.address2 = address2;
14+
return this;
15+
}
16+
17+
public Builder setCity(String city) {
18+
adddress.city = city;
19+
return this;
20+
}
21+
22+
public Builder setPostcode(String postcode) {
23+
adddress.postcode = postcode;
24+
return this;
25+
}
26+
27+
public Builder setCountry(String country) {
28+
adddress.country = country;
29+
return this;
30+
}
31+
32+
public Address build() {
33+
return adddress;
34+
}
35+
}
36+
37+
private String address1;
38+
39+
private String address2;
40+
41+
private String city;
42+
43+
private String postcode;
44+
45+
private String country;
46+
47+
protected Address() {
48+
}
49+
50+
public Address(String address1, String address2, String city, String postcode, String country) {
51+
this.address1 = address1;
52+
this.address2 = address2;
53+
this.city = city;
54+
this.postcode = postcode;
55+
this.country = country;
56+
}
57+
58+
public String getAddress1() {
59+
return address1;
60+
}
61+
62+
public String getAddress2() {
63+
return address2;
64+
}
65+
66+
public String getCity() {
67+
return city;
68+
}
69+
70+
public String getPostcode() {
71+
return postcode;
72+
}
73+
74+
public String getCountry() {
75+
return country;
76+
}
77+
78+
public void setAddress1(String address1) {
79+
this.address1 = address1;
80+
}
81+
82+
public void setAddress2(String address2) {
83+
this.address2 = address2;
84+
}
85+
86+
public void setCity(String city) {
87+
this.city = city;
88+
}
89+
90+
public void setPostcode(String postcode) {
91+
this.postcode = postcode;
92+
}
93+
94+
public void setCountry(String country) {
95+
this.country = country;
96+
}
97+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.roach.data.jpa.department;
2+
3+
import java.util.List;
4+
import java.util.UUID;
5+
6+
import javax.persistence.*;
7+
8+
import org.hibernate.annotations.Type;
9+
import org.hibernate.annotations.TypeDef;
10+
11+
import io.roach.data.jpa.support.AbstractJsonDataType;
12+
13+
@Entity
14+
@Table(name = "department")
15+
@TypeDef(name = "jsonb-users", typeClass = Department.UserCollectionJsonType.class, defaultForType = User.class)
16+
public class Department {
17+
public static class UserCollectionJsonType extends AbstractJsonDataType<User> {
18+
@Override
19+
public Class<User> returnedClass() {
20+
return User.class;
21+
}
22+
23+
@Override
24+
public boolean isCollectionType() {
25+
return true;
26+
}
27+
}
28+
29+
@Id
30+
@GeneratedValue(strategy = GenerationType.AUTO)
31+
private UUID id;
32+
33+
@Type(type = "jsonb-users")
34+
@Column(name = "users")
35+
@Basic(fetch = FetchType.LAZY)
36+
private List<User> users;
37+
38+
public UUID getId() {
39+
return id;
40+
}
41+
42+
public List<User> getUsers() {
43+
return users;
44+
}
45+
46+
public void setUsers(List<User> users) {
47+
this.users = users;
48+
}
49+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.roach.data.jpa.department;
2+
3+
import java.util.UUID;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface DepartmentRepository extends JpaRepository<Department, UUID> {
10+
}

0 commit comments

Comments
 (0)