Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.

Commit a6b7ff7

Browse files
author
Thomas Risberg
committed
Working on examples for Oracle
1 parent f214922 commit a6b7ff7

File tree

9 files changed

+404
-0
lines changed

9 files changed

+404
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.oreilly.springdata.jdbc.domain;
17+
18+
/**
19+
* @author Oliver Gierke
20+
* @author Thomas Risberg
21+
*/
22+
public class AbstractEntity {
23+
24+
private Long id;
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
/*
35+
* (non-Javadoc)
36+
* @see java.lang.Object#equals(java.lang.Object)
37+
*/
38+
@Override
39+
public boolean equals(Object obj) {
40+
41+
if (this == obj) {
42+
return true;
43+
}
44+
45+
if (this.id == null || obj == null || !(this.getClass().equals(obj.getClass()))) {
46+
return false;
47+
}
48+
49+
AbstractEntity that = (AbstractEntity) obj;
50+
51+
return this.id.equals(that.getId());
52+
}
53+
54+
/*
55+
* (non-Javadoc)
56+
* @see java.lang.Object#hashCode()
57+
*/
58+
@Override
59+
public int hashCode() {
60+
return id == null ? 0 : id.hashCode();
61+
}
62+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.oreilly.springdata.jdbc.domain;
2+
3+
/**
4+
*/
5+
public class Address extends AbstractEntity {
6+
private String street, city, country;
7+
8+
public Address() {
9+
}
10+
11+
public Address(String street, String city, String country) {
12+
this.street = street;
13+
this.city = city;
14+
this.country = country;
15+
}
16+
17+
public String getCountry() {
18+
return country;
19+
}
20+
21+
public void setCountry(String country) {
22+
this.country = country;
23+
}
24+
25+
public String getStreet() {
26+
return street;
27+
}
28+
29+
public void setStreet(String street) {
30+
this.street = street;
31+
}
32+
33+
public String getCity() {
34+
return city;
35+
}
36+
37+
public void setCity(String city) {
38+
this.city = city;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return street + ", " + city + " " + country ;
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.oreilly.springdata.jdbc.domain;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
*/
9+
public class Customer extends AbstractEntity {
10+
11+
private String firstName;
12+
private String lastName;
13+
private EmailAddress emailAddress;
14+
private Set<Address> addresses = new HashSet<Address>();
15+
16+
public String getFirstName() {
17+
return firstName;
18+
}
19+
20+
public void setFirstName(String firstName) {
21+
this.firstName = firstName;
22+
}
23+
24+
public String getLastName() {
25+
return lastName;
26+
}
27+
28+
public void setLastName(String lastName) {
29+
this.lastName = lastName;
30+
}
31+
32+
public EmailAddress getEmailAddress() {
33+
return emailAddress;
34+
}
35+
36+
public void setEmailAddress(EmailAddress emailAddress) {
37+
this.emailAddress = emailAddress;
38+
}
39+
40+
public Set<Address> getAddresses() {
41+
return Collections.unmodifiableSet(addresses);
42+
}
43+
44+
public void addAddress(Address address) {
45+
this.addresses.add(address);
46+
}
47+
48+
public void clearAddresses() {
49+
this.addresses.clear();
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Customer: [" + getId() + "] " + firstName + " " + lastName + " " + emailAddress + " " + addresses;
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.oreilly.springdata.jdbc.domain;
2+
3+
import org.springframework.util.Assert;
4+
5+
import java.util.regex.Pattern;
6+
7+
/**
8+
*/
9+
public class EmailAddress {
10+
11+
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
12+
private static final Pattern PATTERN = Pattern.compile(EMAIL_REGEX);
13+
14+
private String value;
15+
16+
protected EmailAddress() {
17+
}
18+
19+
public EmailAddress(String emailAddress) {
20+
Assert.isTrue(isValid(emailAddress), "Invalid email address!");
21+
this.value = emailAddress;
22+
}
23+
24+
public static boolean isValid(String source) {
25+
return PATTERN.matcher(source).matches();
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
if (o == null || getClass() != o.getClass()) return false;
32+
33+
EmailAddress that = (EmailAddress) o;
34+
35+
if (value != null ? !value.equals(that.value) : that.value != null) return false;
36+
37+
return true;
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return value != null ? value.hashCode() : 0;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return value;
48+
}
49+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.oreilly.springdata.jdbc.oracle;
2+
3+
import com.oreilly.springdata.jdbc.domain.Customer;
4+
import com.oreilly.springdata.jdbc.domain.EmailAddress;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author Thomas Risberg
10+
*/
11+
public interface CustomerRepository {
12+
13+
Customer findById(Long id);
14+
15+
List<Customer> findAll();
16+
17+
void save(Customer customer);
18+
19+
void delete(Customer customer);
20+
21+
Customer findByEmailAddress(EmailAddress emailAddress);
22+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.oreilly.springdata.jdbc.oracle;
2+
3+
import com.oreilly.springdata.jdbc.domain.Customer;
4+
import com.oreilly.springdata.jdbc.domain.EmailAddress;
5+
import oracle.jdbc.OracleTypes;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.jdbc.support.oracle.SqlReturnStruct;
8+
import org.springframework.data.jdbc.support.oracle.SqlStructValue;
9+
import org.springframework.jdbc.core.SqlOutParameter;
10+
import org.springframework.jdbc.core.SqlParameter;
11+
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
12+
13+
import javax.sql.DataSource;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
public class StructCustomerRepository implements CustomerRepository {
19+
20+
private SimpleJdbcCall getCustomerCall;
21+
22+
private SimpleJdbcCall saveCustomerCall;
23+
24+
25+
@Autowired
26+
public void setDataSource(DataSource dataSource) {
27+
28+
this.getCustomerCall = new SimpleJdbcCall(dataSource).withProcedureName("get_customer")
29+
.declareParameters(new SqlOutParameter("out_customer",
30+
OracleTypes.STRUCT, "CUSTOMER_TYPE",
31+
new SqlReturnStruct(Customer.class)));
32+
33+
this.saveCustomerCall = new SimpleJdbcCall(dataSource).withProcedureName("save_customer")
34+
.declareParameters(new SqlParameter("in_customer", OracleTypes.STRUCT, "CUSTOMER_TYPE"));
35+
}
36+
37+
@Override
38+
public Customer findById(Long id) {
39+
Map<String, Object> in = new HashMap<String, Object>();
40+
in.put("in_customer_id", id);
41+
return getCustomerCall.executeObject(Customer.class, in);
42+
}
43+
44+
@Override
45+
public List<Customer> findAll() {
46+
return null;
47+
}
48+
49+
@Override
50+
public void save(Customer customer) {
51+
Map<String, Object> in = new HashMap<String, Object>();
52+
in.put("in_customer", new SqlStructValue(customer));
53+
saveCustomerCall.execute(in);
54+
}
55+
56+
@Override
57+
public void delete(Customer customer) {
58+
59+
}
60+
61+
@Override
62+
public Customer findByEmailAddress(EmailAddress emailAddress) {
63+
return null;
64+
}
65+
}

oracle/src/main/resources/META-INF/spring/application-context-oracle.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
<bean class="com.oreilly.springdata.jdbc.oracle.HelloOracle"/>
1717

18+
<bean id="structCustomerRepository" class="com.oreilly.springdata.jdbc.oracle.StructCustomerRepository"/>
19+
1820
</beans>

oracle/src/main/sql/oracle-struct.sql

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE TABLE customer (
2+
id NUMBER(10),
3+
first_name VARCHAR(255),
4+
last_name VARCHAR(255),
5+
email_address VARCHAR(255),
6+
PRIMARY KEY (id));
7+
/
8+
CREATE OR REPLACE TYPE customer_type
9+
AS OBJECT (id NUMBER(10), first_name VARCHAR(255), last_name VARCHAR(255), email_address VARCHAR(255));
10+
/
11+
CREATE OR REPLACE TYPE customer_tab_type AS TABLE OF customer_type;
12+
/
13+
CREATE OR REPLACE PROCEDURE add_customer (in_customer IN customer_type)
14+
AS
15+
BEGIN
16+
INSERT into customer (id, first_name, last_name, email_address) VALUES(in_customer.id, in_customer.first_name, in_customer.last_name , in_customer.email_address);
17+
END;
18+
/
19+
CREATE OR REPLACE PROCEDURE save_customer (in_customer IN customer_type)
20+
AS
21+
m_exists NUMBER;
22+
BEGIN
23+
SELECT count(*) INTO m_exists FROM customer WHERE id = in_customer.id;
24+
IF m_exists > 0 THEN
25+
UPDATE customer SET first_name = in_customer.first_name, last_name = in_customer.last_name, email_address = in_customer.email_address WHERE id = in_customer.id;
26+
ELSE
27+
INSERT into customer (id, first_name, last_name, email_address) VALUES(in_customer.id, in_customer.first_name, in_customer.last_name , in_customer.email_address);
28+
END IF;
29+
END;
30+
/
31+
CREATE OR REPLACE PROCEDURE get_customer (in_customer_id IN NUMBER, out_customer OUT customer_type)
32+
AS
33+
BEGIN
34+
SELECT customer_type(id, first_name, last_name, email_address) INTO out_customer FROM customer WHERE id = in_customer_id;
35+
END;
36+
/
37+
CREATE OR REPLACE FUNCTION get_all_customer_types RETURN customer_tab_type
38+
AS
39+
m_customer_tab customer_tab_type;
40+
cursor c_customer is
41+
select id, first_name, last_name, email_address from customer a;
42+
BEGIN
43+
m_customer_tab := customer_tab_type();
44+
FOR r_customer IN c_customer loop
45+
m_customer_tab.extend;
46+
m_customer_tab(m_customer_tab.count) := customer_type(r_customer.id, r_customer.first_name, r_customer.last_name, r_customer.email_address);
47+
END LOOP;
48+
RETURN m_customer_tab;
49+
END;
50+
/

0 commit comments

Comments
 (0)