Skip to content

Commit 73628a2

Browse files
Added sample code - ODSA for Java Devs - Spring Boot 3 + Data JPA P2 (#229)
* Added sample code - ODSA for Java Devs - Spring Boot 3 + Data JPA P2 * adjusted the number of initial connections * code sample adjustments * code sample adjustments * fix indentation * adjustment accepted, debugging properties removed * change year to 2024 + remove serial id attribute * update jdbc driver version to 23.6.0.24.10, address target dir * full technical explanation of topics --------- Co-authored-by: Jean de Lavarene <jean.de.lavarene@oracle.com>
1 parent c3bd771 commit 73628a2

File tree

10 files changed

+489
-0
lines changed

10 files changed

+489
-0
lines changed

java/odsa-spring/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# odsa-spring
2+
[ODSA for Java Developers (Part 2) — Connecting to Oracle ADB from a Spring Boot 3 App with Spring Data JPA on Azure App Service](https://medium.com/oracledevs/odsa-for-java-developers-part-2-connecting-to-oracle-adb-from-a-spring-boot-3-0-b1ebb6296abc)

java/odsa-spring/pom.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.3.0</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.oracle.dev.jdbc</groupId>
13+
<artifactId>odsa-spring</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>odsa-spring</name>
16+
<description>ODSA for Java Developers - Connecting to Oracle ADB from a Spring
17+
Boot App on Azure</description>
18+
<properties>
19+
<java.version>21</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.oracle.database.jdbc</groupId>
41+
<artifactId>ojdbc11-production</artifactId>
42+
<version>23.6.0.24.10</version>
43+
<type>pom</type>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.hibernate.orm.tooling</groupId>
51+
<artifactId>hibernate-enhance-maven-plugin</artifactId>
52+
<version>${hibernate.version}</version>
53+
<executions>
54+
<execution>
55+
<id>enhance</id>
56+
<goals>
57+
<goal>enhance</goal>
58+
</goals>
59+
<configuration>
60+
<enableLazyInitialization>true</enableLazyInitialization>
61+
<enableDirtyTracking>true</enableDirtyTracking>
62+
<enableAssociationManagement>true</enableAssociationManagement>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.graalvm.buildtools</groupId>
69+
<artifactId>native-maven-plugin</artifactId>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
</plugin>
75+
76+
77+
<!-- Azure App Service -->
78+
<plugin>
79+
<groupId>com.microsoft.azure</groupId>
80+
<artifactId>azure-webapp-maven-plugin</artifactId>
81+
<version>2.5.0</version>
82+
<configuration>
83+
<subscriptionId>YOUR_AZURE_SUBSCRIPTION_ID</subscriptionId>
84+
<resourceGroup>DBIntegration.Multicloud.ResourceGroup</resourceGroup>
85+
<appName>odsa-spring</appName>
86+
<pricingTier>B2</pricingTier>
87+
<region>westus</region>
88+
<runtime>
89+
<os>Linux</os>
90+
<webContainer>Java SE</webContainer>
91+
<javaVersion>Java 17</javaVersion>
92+
</runtime>
93+
<deployment>
94+
<resources>
95+
<resource>
96+
<type>jar</type>
97+
<directory>${project.basedir}/target</directory>
98+
<includes>
99+
<include>*.jar</include>
100+
</includes>
101+
</resource>
102+
</resources>
103+
</deployment>
104+
</configuration>
105+
</plugin>
106+
107+
</plugins>
108+
</build>
109+
110+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
CREATE USER ODSA_USER IDENTIFIED BY <YOUR_PASSWORD>;
2+
ALTER USER ODSA_USER QUOTA 128M ON USERS;
3+
ALTER SESSION SET CURRENT_SCHEMA = ODSA_USER;
4+
5+
CREATE TABLE Employee
6+
(id NUMBER(10) CONSTRAINT pk_employee PRIMARY KEY,
7+
name VARCHAR2(20),
8+
job VARCHAR2(20),
9+
salary NUMBER(10),
10+
commission NUMBER(10));
11+
12+
INSERT INTO Employee VALUES(7369,'JOHN','CLERK',7902,NULL);
13+
INSERT INTO Employee VALUES(7499,'PETER','SALESMAN',7698,300);
14+
INSERT INTO Employee VALUES(7521,'JEFF','SALESMAN',7698,500);
15+
INSERT INTO Employee VALUES(7566,'MARK','MANAGER',7839,NULL);
16+
INSERT INTO Employee VALUES(7654,'MARTIN','SALESMAN',7698,1400);
17+
INSERT INTO Employee VALUES(7698,'ADAM','MANAGER',7839,NULL);
18+
INSERT INTO Employee VALUES(7782,'CLARK','MANAGER',7839,NULL);
19+
INSERT INTO Employee VALUES(7788,'SCOTT','ANALYST',7566,NULL);
20+
INSERT INTO Employee VALUES(7839,'KING','PRESIDENT',NULL);
21+
INSERT INTO Employee VALUES(7844,'TURNER','SALESMAN',0);
22+
INSERT INTO Employee VALUES(7876,'ADAMS','CLERK',7788,NULL);
23+
INSERT INTO Employee VALUES(7900,'JAMES','CLERK',7698,NULL);
24+
INSERT INTO Employee VALUES(7902,'FORD','ANALYST',7566,NULL);
25+
INSERT INTO Employee VALUES(7934,'MILLER','CLERK',7782,NULL);
26+
27+
COMMIT;
28+
29+
30+
31+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc.odsa.spring;
23+
24+
import java.util.Objects;
25+
26+
import jakarta.persistence.Entity;
27+
import jakarta.persistence.GeneratedValue;
28+
import jakarta.persistence.Id;
29+
import jakarta.persistence.Table;
30+
31+
@Entity
32+
@Table(name = "EMPLOYEE", schema = "<DB_SCHEMA>")
33+
public class Employee {
34+
35+
private @Id @GeneratedValue Long id;
36+
private String name;
37+
private String job;
38+
private Integer salary;
39+
private Integer commission;
40+
41+
public Employee() {
42+
}
43+
44+
public Employee(Long id, String name, String job, Integer salary,
45+
Integer commission) {
46+
this.id = id;
47+
this.name = name;
48+
this.job = job;
49+
this.salary = salary;
50+
this.commission = commission;
51+
}
52+
53+
public Long getId() {
54+
return id;
55+
}
56+
57+
public void setId(Long id) {
58+
this.id = id;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public void setName(String name) {
66+
this.name = name;
67+
}
68+
69+
public String getJob() {
70+
return job;
71+
}
72+
73+
public void setJob(String job) {
74+
this.job = job;
75+
}
76+
77+
public Integer getSalary() {
78+
return salary;
79+
}
80+
81+
public void setSalary(Integer salary) {
82+
this.salary = salary;
83+
}
84+
85+
public Integer getCommission() {
86+
return commission;
87+
}
88+
89+
public void setCommission(Integer commission) {
90+
this.commission = commission;
91+
}
92+
93+
@Override
94+
public int hashCode() {
95+
return Objects.hash(commission, id, job, name, salary);
96+
}
97+
98+
@Override
99+
public boolean equals(Object obj) {
100+
if (this == obj)
101+
return true;
102+
if (obj == null)
103+
return false;
104+
if (getClass() != obj.getClass())
105+
return false;
106+
Employee other = (Employee) obj;
107+
return Objects.equals(commission, other.commission)
108+
&& Objects.equals(id, other.id) && Objects.equals(job, other.job)
109+
&& Objects.equals(name, other.name)
110+
&& Objects.equals(salary, other.salary);
111+
}
112+
113+
public String toString() {
114+
return String.format("%20s %20s %20s %20s %20s", id, name, job, salary,
115+
commission);
116+
117+
}
118+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc.odsa.spring;
23+
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
27+
@SpringBootApplication
28+
public class EmployeeApplication {
29+
30+
public static void main(String[] args) {
31+
SpringApplication.run(EmployeeApplication.class, args);
32+
}
33+
34+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright (c) 2024, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc.odsa.spring;
23+
24+
import java.util.List;
25+
26+
import org.springframework.web.bind.annotation.DeleteMapping;
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.PathVariable;
29+
import org.springframework.web.bind.annotation.PostMapping;
30+
import org.springframework.web.bind.annotation.PutMapping;
31+
import org.springframework.web.bind.annotation.RequestBody;
32+
import org.springframework.web.bind.annotation.RestController;
33+
34+
@RestController
35+
class EmployeeController {
36+
37+
private final EmployeeRepository repository;
38+
39+
EmployeeController(EmployeeRepository repository) {
40+
this.repository = repository;
41+
}
42+
43+
// Aggregate root
44+
// tag::get-aggregate-root[]
45+
@GetMapping("/employees")
46+
List<Employee> all() {
47+
return repository.findAll();
48+
}
49+
// end::get-aggregate-root[]
50+
51+
@PostMapping("/employees")
52+
Employee newEmployee(@RequestBody Employee newEmployee) {
53+
return repository.save(newEmployee);
54+
}
55+
56+
// Single item
57+
58+
@GetMapping("/employees/{id}")
59+
Employee one(@PathVariable Long id) {
60+
61+
return repository.findById(id)
62+
.orElseThrow(() -> new EmployeeNotFoundException(id));
63+
}
64+
65+
@PutMapping("/employees/{id}")
66+
Employee replaceEmployee(@RequestBody Employee newEmployee,
67+
@PathVariable Long id) {
68+
69+
return repository.findById(id).map(employee -> {
70+
employee.setName(newEmployee.getName());
71+
employee.setJob(newEmployee.getJob());
72+
return repository.save(employee);
73+
}).orElseGet(() -> {
74+
newEmployee.setId(id);
75+
return repository.save(newEmployee);
76+
});
77+
}
78+
79+
@DeleteMapping("/employees/{id}")
80+
void deleteEmployee(@PathVariable Long id) {
81+
repository.deleteById(id);
82+
}
83+
}

0 commit comments

Comments
 (0)