Skip to content

JSON Duality Example #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Oracle Spring Boot Sample for JSON Relational Duality Views

This sample application demonstrates how to use the Oracle Spring Boot Starter JSON Collections with [JSON Relational Duality Views](https://docs.oracle.com/en/database/oracle/oracle-database/23/jsnvu/overview-json-relational-duality-views.html)

The Oracle Spring Boot Sample for JSON Relational Duality Views package includes the following components to demonstrate development with JSON Relational Duality Views from a Spring Boot Java context:

- Entities for JSON Relational Duality Views (Student, Enrollment, Course, Lecture Hall)
- Services to interact with the JSON Relational Duality Views
- A SQL script that initializes the database, including the JSON Relational Duality Views.
- A comprehensive test that uses Spring Boot services to manipulate data from JSON Relational Duality Views.

## Run the sample application

The sample application creates a temporary Oracle Free container database, and requires a docker runtime environment.

To run the test application, run the following command:

```shell
mvn test
```

## Configure your project to use Oracle JSON Relational Duality Views

To use Oracle JSON Relational Duality Views from your Spring Boot application, add the following Maven dependency to your project:

```xml
<dependency>
<groupId>com.oracle.database.spring</groupId>
<artifactId>oracle-spring-boot-starter-json-collections</artifactId>
</dependency>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2024, Oracle and/or its affiliates. -->
<!-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>oracle-spring-boot-starter-samples</artifactId>
<groupId>com.oracle.database.spring</groupId>
<version>24.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>oracle-spring-boot-sample-json-duality</artifactId>
<version>24.2.0</version>

<name>Oracle Spring Boot Starter - JSON Relational Duality Views Sample</name>
<description>Oracle Spring Boot Starter Sample for JSON Relational Duality Views</description>

<organization>
<name>Oracle America, Inc.</name>
<url>https://www.oracle.com</url>
</organization>

<developers>
<developer>
<name>Oracle</name>
<email>obaas_ww at oracle.com</email>
<organization>Oracle America, Inc.</organization>
<organizationUrl>https://www.oracle.com</organizationUrl>
</developer>
</developers>

<licenses>
<license>
<name>The Universal Permissive License (UPL), Version 1.0</name>
<url>https://oss.oracle.com/licenses/upl/</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<url>https://github.com/oracle/spring-cloud-oracle</url>
<connection>scm:git:https://github.com/oracle/spring-cloud-oracle.git</connection>
<developerConnection>scm:git:[email protected]:oracle/spring-cloud-oracle.git</developerConnection>
</scm>

<dependencies>
<dependency>
<groupId>com.oracle.database.spring</groupId>
<artifactId>oracle-spring-boot-starter-json-collections</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-dependencies.version}</version>
<scope>test</scope>
</dependency>

<!-- Test Dependencies-->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-free</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.database.spring.jsonduality;

import java.util.UUID;

public class Course {
private String _id;
private String name;
private String description;
private Integer credits;
private LectureHall lecture_hall;

public static Course createCourse() {
Course course = new Course();
course.set_id(UUID.randomUUID().toString());
return course;
}

public Course() {
}

public Course(String _id, String name, String description, Integer credits, LectureHall lecture_hall) {
this._id = _id;
this.name = name;
this.description = description;
this.credits = credits;
this.lecture_hall = lecture_hall;
}

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getCredits() {
return credits;
}

public void setCredits(Integer credits) {
this.credits = credits;
}

public LectureHall getLecture_hall() {
return lecture_hall;
}

public void setLecture_hall(LectureHall lecture_hall) {
this.lecture_hall = lecture_hall;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.database.spring.jsonduality;

import java.sql.PreparedStatement;
import java.util.List;

import com.oracle.spring.json.jsonb.JSONB;
import com.oracle.spring.json.jsonb.JSONBRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component
public class CourseService {
private static final String byName = """
select * from courses_dv v
where v.data.name = ?
""";

private final JdbcTemplate jdbcTemplate;
private final JSONB jsonb;
private final RowMapper<Course> rowMapper;

public CourseService(JdbcTemplate jdbcTemplate, JSONB jsonb) {
this.jdbcTemplate = jdbcTemplate;
this.jsonb = jsonb;
rowMapper = new JSONBRowMapper<>(jsonb, Course.class);
}

public List<Course> getCourseByName(String name) {
return jdbcTemplate.query(con -> {
PreparedStatement ps = con.prepareStatement(byName);
ps.setString(1, name);
return ps;
}, rowMapper);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.database.spring.jsonduality;

import java.util.UUID;

public class Enrollment {
private String _id;
private Course course;

public Enrollment createEnrollment() {
Enrollment enrollment = new Enrollment();
enrollment.set_id(UUID.randomUUID().toString());
return enrollment;
}

public Enrollment() {}

public Enrollment(String _id, Course course) {
this._id = _id;
this.course = course;
}

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

public Course getCourse() {
return course;
}

public void setCourse(Course course) {
this.course = course;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.database.spring.jsonduality;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class JSONDualitySampleApplication {
public static void main(String[] args) {
SpringApplication.run(JSONDualitySampleApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package com.oracle.database.spring.jsonduality;

import java.util.UUID;

public class LectureHall {
private String _id;
private String name;

public static LectureHall createLecureHall() {
LectureHall hall = new LectureHall();
hall.set_id(UUID.randomUUID().toString());
return hall;
}

public LectureHall() {}

public LectureHall(String _id, String name) {
this._id = _id;
this.name = name;
}

public String get_id() {
return _id;
}

public void set_id(String _id) {
this._id = _id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Loading
Loading