Skip to content

Commit beb74da

Browse files
JSON Duality Example
Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 4b6eff3 commit beb74da

File tree

13 files changed

+730
-0
lines changed

13 files changed

+730
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Oracle Spring Boot Sample for JSON Duality Views
2+
3+
This sample application demonstrates how to use the Oracle Spring Boot Starter JSON Collections with [JSON Duality Views](https://docs.oracle.com/en/database/oracle/oracle-database/23/jsnvu/overview-json-relational-duality-views.html)
4+
5+
The Oracle Spring Boot Sample for JSON Duality Views package includes the following components to demonstrate development with JSON Duality Views from a Spring Boot Java context:
6+
7+
- Entities for JSON duality views (Student, Enrollment, Course, Lecture Hall)
8+
- Services to interact with the JSON duality views
9+
- A SQL script that initializes the database, including the JSON duality views.
10+
- A comprehensive test that uses Spring Boot services to manipulate data from JSON duality views.
11+
12+
## Run the sample application
13+
14+
The sample application creates a temporary Oracle Free container database, and requires a docker runtime environment.
15+
16+
To run the test application, run the following command:
17+
18+
```shell
19+
mvn test
20+
```
21+
22+
## Configure your project to use Oracle JSON Duality Views
23+
24+
To use Oracle JSON Duality Views from your Spring Boot application, add the following Maven dependency to your project:
25+
26+
```xml
27+
<dependency>
28+
<groupId>com.oracle.database.spring</groupId>
29+
<artifactId>oracle-spring-boot-starter-json-collections</artifactId>
30+
</dependency>
31+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Copyright (c) 2024, Oracle and/or its affiliates. -->
3+
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
4+
<project xmlns="http://maven.apache.org/POM/4.0.0"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
7+
<modelVersion>4.0.0</modelVersion>
8+
<parent>
9+
<artifactId>oracle-spring-boot-starter-samples</artifactId>
10+
<groupId>com.oracle.database.spring</groupId>
11+
<version>24.2.0</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
14+
15+
<artifactId>oracle-spring-boot-sample-json-duality</artifactId>
16+
<version>24.2.0</version>
17+
18+
<name>Oracle Spring Boot Starter - JSON Duality Views Sample</name>
19+
<description>Oracle Spring Boot Starter Sample for JSON Duality Views</description>
20+
21+
<organization>
22+
<name>Oracle America, Inc.</name>
23+
<url>https://www.oracle.com</url>
24+
</organization>
25+
26+
<developers>
27+
<developer>
28+
<name>Oracle</name>
29+
<email>obaas_ww at oracle.com</email>
30+
<organization>Oracle America, Inc.</organization>
31+
<organizationUrl>https://www.oracle.com</organizationUrl>
32+
</developer>
33+
</developers>
34+
35+
<licenses>
36+
<license>
37+
<name>The Universal Permissive License (UPL), Version 1.0</name>
38+
<url>https://oss.oracle.com/licenses/upl/</url>
39+
<distribution>repo</distribution>
40+
</license>
41+
</licenses>
42+
43+
<scm>
44+
<url>https://github.com/oracle/spring-cloud-oracle</url>
45+
<connection>scm:git:https://github.com/oracle/spring-cloud-oracle.git</connection>
46+
<developerConnection>scm:git:git@github.com:oracle/spring-cloud-oracle.git</developerConnection>
47+
</scm>
48+
49+
<dependencies>
50+
<dependency>
51+
<groupId>com.oracle.database.spring</groupId>
52+
<artifactId>oracle-spring-boot-starter-json-collections</artifactId>
53+
<version>${project.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-starter</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-web</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-test</artifactId>
71+
<version>${spring-boot-dependencies.version}</version>
72+
<scope>test</scope>
73+
</dependency>
74+
75+
<!-- Test Dependencies-->
76+
<dependency>
77+
<groupId>org.testcontainers</groupId>
78+
<artifactId>junit-jupiter</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
82+
<dependency>
83+
<groupId>org.testcontainers</groupId>
84+
<artifactId>testcontainers</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.testcontainers</groupId>
90+
<artifactId>oracle-free</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
</dependencies>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
package com.oracle.database.spring.jsonduality;
4+
5+
import java.util.UUID;
6+
7+
public class Course {
8+
private String _id;
9+
private String name;
10+
private String description;
11+
private Integer credits;
12+
private LectureHall lecture_hall;
13+
14+
public static Course createCourse() {
15+
Course course = new Course();
16+
course.set_id(UUID.randomUUID().toString());
17+
return course;
18+
}
19+
20+
public Course() {
21+
}
22+
23+
public Course(String _id, String name, String description, Integer credits, LectureHall lecture_hall) {
24+
this._id = _id;
25+
this.name = name;
26+
this.description = description;
27+
this.credits = credits;
28+
this.lecture_hall = lecture_hall;
29+
}
30+
31+
public String get_id() {
32+
return _id;
33+
}
34+
35+
public void set_id(String _id) {
36+
this._id = _id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public String getDescription() {
48+
return description;
49+
}
50+
51+
public void setDescription(String description) {
52+
this.description = description;
53+
}
54+
55+
public Integer getCredits() {
56+
return credits;
57+
}
58+
59+
public void setCredits(Integer credits) {
60+
this.credits = credits;
61+
}
62+
63+
public LectureHall getLecture_hall() {
64+
return lecture_hall;
65+
}
66+
67+
public void setLecture_hall(LectureHall lecture_hall) {
68+
this.lecture_hall = lecture_hall;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
package com.oracle.database.spring.jsonduality;
4+
5+
import java.sql.PreparedStatement;
6+
import java.util.List;
7+
8+
import com.oracle.spring.json.jsonb.JSONB;
9+
import com.oracle.spring.json.jsonb.JSONBRowMapper;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.jdbc.core.RowMapper;
12+
import org.springframework.stereotype.Component;
13+
14+
@Component
15+
public class CourseService {
16+
private static final String byName = """
17+
select * from courses_dv v
18+
where v.data.name = ?
19+
""";
20+
21+
private final JdbcTemplate jdbcTemplate;
22+
private final JSONB jsonb;
23+
private final RowMapper<Course> rowMapper;
24+
25+
public CourseService(JdbcTemplate jdbcTemplate, JSONB jsonb) {
26+
this.jdbcTemplate = jdbcTemplate;
27+
this.jsonb = jsonb;
28+
rowMapper = new JSONBRowMapper<>(jsonb, Course.class);
29+
}
30+
31+
public List<Course> getCourseByName(String name) {
32+
return jdbcTemplate.query(con -> {
33+
PreparedStatement ps = con.prepareStatement(byName);
34+
ps.setString(1, name);
35+
return ps;
36+
}, rowMapper);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
package com.oracle.database.spring.jsonduality;
4+
5+
import java.util.UUID;
6+
7+
public class Enrollment {
8+
private String _id;
9+
private Course course;
10+
11+
public Enrollment createEnrollment() {
12+
Enrollment enrollment = new Enrollment();
13+
enrollment.set_id(UUID.randomUUID().toString());
14+
return enrollment;
15+
}
16+
17+
public Enrollment() {}
18+
19+
public Enrollment(String _id, Course course) {
20+
this._id = _id;
21+
this.course = course;
22+
}
23+
24+
public String get_id() {
25+
return _id;
26+
}
27+
28+
public void set_id(String _id) {
29+
this._id = _id;
30+
}
31+
32+
public Course getCourse() {
33+
return course;
34+
}
35+
36+
public void setCourse(Course course) {
37+
this.course = course;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
package com.oracle.database.spring.jsonduality;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class JSONDualitySampleApplication {
11+
public static void main(String[] args) {
12+
SpringApplication.run(JSONDualitySampleApplication.class, args);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
package com.oracle.database.spring.jsonduality;
4+
5+
import java.util.UUID;
6+
7+
public class LectureHall {
8+
private String _id;
9+
private String name;
10+
11+
public static LectureHall createLecureHall() {
12+
LectureHall hall = new LectureHall();
13+
hall.set_id(UUID.randomUUID().toString());
14+
return hall;
15+
}
16+
17+
public LectureHall() {}
18+
19+
public LectureHall(String _id, String name) {
20+
this._id = _id;
21+
this.name = name;
22+
}
23+
24+
public String get_id() {
25+
return _id;
26+
}
27+
28+
public void set_id(String _id) {
29+
this._id = _id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
}

0 commit comments

Comments
 (0)