Skip to content

Commit ca189b6

Browse files
UCP+JPA Sample (#104)
* UCP+JPA Sample --------- Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 6cc4db8 commit ca189b6

File tree

12 files changed

+497
-11
lines changed

12 files changed

+497
-11
lines changed

database/spring-cloud-stream-binder-oracle-txeventq/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# TxEventQ support for Spring Cloud Stream
1+
# [TxEventQ](https://www.oracle.com/database/advanced-queuing/) support for Spring Cloud Stream
22

33
This version of the binder supports Spring Boot 3+/Spring framework 6+.
44

5+
For more information on queuing within Oracle Database, see the official documentation for [Transactional Event Queues](https://www.oracle.com/database/advanced-queuing/).
6+
57
## Getting started
68

79
Install the project after cloning this repo.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Oracle Spring Boot Sample UCP JPA
2+
3+
This sample application demonstrates how to use the Oracle Spring Boot Starter UCP with Spring Data JPA, connecting your Oracle Database with powerful ORM abstractions that facilitate rapid development.
4+
5+
The Oracle Spring Boot Sample UCP JPA package includes a JPA entity, repository, and rest controller to interact with the JPA repository. All necessary configuration and dependencies are bootstrapped, with an end-to-end test demonstrating the functionality of Spring JPA with Oracle Database and UCP.
6+
7+
## Run the sample application
8+
9+
The sample application creates a temporary Oracle Free container database, and requires a docker runtime environment. The sample application demonstrates the use of Spring Data JPA with the Oracle Spring Boot Starter UCP.
10+
11+
To run the test application, run the following command:
12+
13+
```shell
14+
mvn test
15+
```
16+
17+
## Configure Maven dependencies to use Oracle UCP and Spring Data JPA
18+
19+
```xml
20+
<dependency>
21+
<groupId>com.oracle.database.spring</groupId>
22+
<artifactId>oracle-spring-boot-starter-ucp</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-data-jpa</artifactId>
40+
</dependency>
41+
```
42+
43+
## Configure application properties to use Oracle UCP as the datasource provider
44+
45+
```yaml
46+
spring:
47+
jpa:
48+
hibernate:
49+
ddl-auto: none
50+
datasource:
51+
username: ${USERNAME}
52+
password: ${PASSWORD}
53+
url: ${JDBC_URL}
54+
55+
# Set these to use UCP over Hikari.
56+
driver-class-name: oracle.jdbc.OracleDriver
57+
type: oracle.ucp.jdbc.PoolDataSourceImpl
58+
oracleucp:
59+
initial-pool-size: 1
60+
min-pool-size: 1
61+
max-pool-size: 30
62+
connection-pool-name: UCPSampleApplication
63+
connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
64+
```
65+
66+
## Write a JPA repository and entity
67+
68+
```java
69+
import org.springframework.data.jpa.repository.JpaRepository;
70+
71+
public interface StudentRepository extends JpaRepository<Student, String> {}
72+
```
73+
74+
```java
75+
import jakarta.persistence.Column;
76+
import jakarta.persistence.Entity;
77+
import jakarta.persistence.GeneratedValue;
78+
import jakarta.persistence.GenerationType;
79+
import jakarta.persistence.Id;
80+
import jakarta.persistence.Table;
81+
82+
@Entity
83+
@Table(name = "STUDENT")
84+
public class Student {
85+
@Id
86+
@GeneratedValue(strategy = GenerationType.UUID)
87+
private String id;
88+
@Column(name = "first_name")
89+
private String firstName;
90+
@Column(name = "last_name")
91+
private String lastName;
92+
private String email;
93+
private String major;
94+
private double credits;
95+
private double gpa;
96+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.1.0</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
14+
15+
<artifactId>oracle-spring-boot-sample-ucp-jpa</artifactId>
16+
<version>24.1.0</version>
17+
18+
<name>Oracle Spring Boot Starter - UCP with JPA Sample</name>
19+
<description>Oracle Spring Boot Starter Sample UCP with JPA</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-ucp</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+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-data-jpa</artifactId>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-test</artifactId>
75+
<version>${spring-boot-dependencies.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<!-- Test Dependencies-->
80+
<dependency>
81+
<groupId>org.testcontainers</groupId>
82+
<artifactId>junit-jupiter</artifactId>
83+
<scope>test</scope>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>org.testcontainers</groupId>
88+
<artifactId>testcontainers</artifactId>
89+
<scope>test</scope>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>org.testcontainers</groupId>
94+
<artifactId>oracle-free</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
</dependencies>
98+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.sample;
4+
5+
import jakarta.persistence.Column;
6+
import jakarta.persistence.Entity;
7+
import jakarta.persistence.GeneratedValue;
8+
import jakarta.persistence.GenerationType;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "STUDENT")
14+
public class Student {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.UUID)
17+
private String id;
18+
@Column(name = "first_name")
19+
private String firstName;
20+
@Column(name = "last_name")
21+
private String lastName;
22+
private String email;
23+
private String major;
24+
private double credits;
25+
private double gpa;
26+
27+
public Student() {}
28+
29+
public Student(String firstName, String lastName, String email, String major, double credits, double gpa) {
30+
this.firstName = firstName;
31+
this.lastName = lastName;
32+
this.email = email;
33+
this.major = major;
34+
this.credits = credits;
35+
this.gpa = gpa;
36+
}
37+
38+
public String getId() {
39+
return id;
40+
}
41+
42+
public void setId(String id) {
43+
this.id = id;
44+
}
45+
46+
public String getFirstName() {
47+
return firstName;
48+
}
49+
50+
public void setFirstName(String firstName) {
51+
this.firstName = firstName;
52+
}
53+
54+
public String getLastName() {
55+
return lastName;
56+
}
57+
58+
public void setLastName(String lastName) {
59+
this.lastName = lastName;
60+
}
61+
62+
public String getEmail() {
63+
return email;
64+
}
65+
66+
public void setEmail(String email) {
67+
this.email = email;
68+
}
69+
70+
public String getMajor() {
71+
return major;
72+
}
73+
74+
public void setMajor(String major) {
75+
this.major = major;
76+
}
77+
78+
public double getCredits() {
79+
return credits;
80+
}
81+
82+
public void setCredits(double credits) {
83+
this.credits = credits;
84+
}
85+
86+
public double getGpa() {
87+
return gpa;
88+
}
89+
90+
public void setGpa(double gpa) {
91+
this.gpa = gpa;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.sample;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
import org.springframework.http.HttpStatusCode;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.DeleteMapping;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
@RestController
19+
@RequestMapping("/student")
20+
public class StudentController {
21+
private final StudentRepository studentRepository;
22+
23+
public StudentController(StudentRepository studentRepository) {
24+
this.studentRepository = studentRepository;
25+
}
26+
27+
@GetMapping
28+
public ResponseEntity<List<Student>> listStudents() {
29+
return ResponseEntity.ok(studentRepository.findAll());
30+
}
31+
32+
@PostMapping
33+
public ResponseEntity<Student> createStudent(@RequestBody Student student) {
34+
Student created = studentRepository.save(student);
35+
return new ResponseEntity<>(created, HttpStatusCode.valueOf(201));
36+
}
37+
38+
@GetMapping("/{studentId}")
39+
public ResponseEntity<Student> getStudent(@PathVariable String studentId) {
40+
Optional<Student> student = studentRepository.findById(studentId);
41+
return student.map(ResponseEntity::ok).orElseGet(() ->
42+
ResponseEntity.notFound().build()
43+
);
44+
}
45+
46+
@DeleteMapping("/{studentId}")
47+
public ResponseEntity<?> deleteStudent(@PathVariable String studentId) {
48+
studentRepository.deleteById(studentId);
49+
return ResponseEntity.noContent().build();
50+
}
51+
}
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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.sample;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
7+
public interface StudentRepository extends JpaRepository<Student, String> {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.sample;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
@SpringBootApplication
9+
public class UCPSampleApplication {
10+
public static void main(String[] args) {
11+
SpringApplication.run(UCPSampleApplication.class, args);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
spring:
2+
jpa:
3+
hibernate:
4+
ddl-auto: none
5+
datasource:
6+
username: ${USERNAME}
7+
password: ${PASSWORD}
8+
url: ${JDBC_URL}
9+
10+
# Set these to use UCP over Hikari.
11+
driver-class-name: oracle.jdbc.OracleDriver
12+
type: oracle.ucp.jdbc.PoolDataSourceImpl
13+
oracleucp:
14+
initial-pool-size: 1
15+
min-pool-size: 1
16+
max-pool-size: 30
17+
connection-pool-name: UCPSampleApplication
18+
connection-factory-class-name: oracle.jdbc.pool.OracleDataSource
19+
server:
20+
port: 9001

0 commit comments

Comments
 (0)