Spring, Hibernate and Oracle Stored Procedures
2015-06-18 14:34
417 查看
一篇英文博文,写的是利用hibernate处理存储过程中的游标等等:
Motivation: While there are a few resources available online for calling stored procedures from Hibernate, it took me a while to stumble across one that mostly captures what I need. The intention of this blog entry is to put a similar example into my own words, to extend it slightly and hopefully to help anyone not experienced with Hibernate and Oracle to integrate Stored Procedures and Functions into an application quickly.
Setup Oracle 10g
For this example, we will be using Oracle 10g. We can initialize our schema user with SQLPlus with the following commands:
sqlplus connect as sysdba
create user my_orcl identified by my_orcl;
grant create session to my_orcl;
grant resource to my_orcl;
grant create table to my_orcl;
Setup a Project For Spring and Hibernate
We will download spring-framework-2.5.5-with-dependencies.zip, hibernate-distribution-3.3.1.GA-dist.zip and hibernate-annotations-3.4.0.GA.zip. We can create a standard project layout of src, test and lib folders with the following jars on the classpath:
spring-framework-2.5.5/dist/spring.jar
spring-framework-2.5.5/dist/modules/spring-test.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-logging.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-dbcp.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-pool.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-collections.jar
spring-framework-2.5.5/lib/dom4j/dom4j-1.6.1.jar
spring-framework-2.5.5/lib/log4j/log4j-1.2.15.jar
spring-framework-2.5.5/lib/slf4j/slf4j-api-1.5.0.jar
spring-framework-2.5.5/lib/slf4j/slf4j-log4j12-1.5.0.jar
spring-framework-2.5.5/lib/j2ee/*.jar
hibernate-annotations-3.4.0.GA/hibernate-annotations.jar
hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
hibernate-distribution-3.3.1.GA/hibernate3.jar
hibernate-distribution-3.3.1.GA/lib/required/javassist-3.4.GA.jar
hibernate-distribution-3.3.1.GA/lib/required/slf4j-api-1.5.2.jar
Because we will be using Oracle Stored Procedures, we will also need a database driver such as
oracle/product/10.2.0/db_1/jdbc/lib/ojdbc14.jar
Create Domain Objects
We can setup our domain using annotated Java. For these examples, we need one simple domain Object.
package spring.hibernate.oracle.stored.procedures.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = “AUTHOR”, schema = “MY_ORCL”)
public class Author implements java.io.Serializable {
private static final long serialVersionUID = 8676058601610931698L;
private int id;
private String firstName;
private String lastName;
@Id
@Column(name = “ID”, nullable = false)
public int getId() {
return this.id;
}
public void setId(final int id) {
this.id = id;
}
@Column(name = “FIRST_NAME”, nullable = false, length = 50)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
@Column(name = “LAST_NAME”, nullable = false, length = 50)
public String getLastName() {
return this.lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
}
Create a DAO
Now that we have a domain Object, we can create a DAO for a simple operation, such as looking up Authors by last name. Fortunately, Spring provides a convenient base class for DAO operations.
package spring.hibernate.oracle.stored.procedures.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import spring.hibernate.oracle.stored.procedures.domain.Author;
public class AuthorDAO extends HibernateDaoSupport {
@SuppressWarnings(“unchecked”)
public List findByLastNameUsingHQL(final String lastName) {
return getHibernateTemplate().find(“from Author author where author.lastName = ?”, lastName);
}
}
The Spring Application Context Configuration
The Spring applicationContext.xml can reside directly at the root of our src classpath, and it will contain information for configuring Spring to manage our Hibernate sessions, transactions and datasources, as well as our DAO.
Motivation: While there are a few resources available online for calling stored procedures from Hibernate, it took me a while to stumble across one that mostly captures what I need. The intention of this blog entry is to put a similar example into my own words, to extend it slightly and hopefully to help anyone not experienced with Hibernate and Oracle to integrate Stored Procedures and Functions into an application quickly.
Setup Oracle 10g
For this example, we will be using Oracle 10g. We can initialize our schema user with SQLPlus with the following commands:
sqlplus connect as sysdba
create user my_orcl identified by my_orcl;
grant create session to my_orcl;
grant resource to my_orcl;
grant create table to my_orcl;
Setup a Project For Spring and Hibernate
We will download spring-framework-2.5.5-with-dependencies.zip, hibernate-distribution-3.3.1.GA-dist.zip and hibernate-annotations-3.4.0.GA.zip. We can create a standard project layout of src, test and lib folders with the following jars on the classpath:
spring-framework-2.5.5/dist/spring.jar
spring-framework-2.5.5/dist/modules/spring-test.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-logging.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-dbcp.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-pool.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-collections.jar
spring-framework-2.5.5/lib/dom4j/dom4j-1.6.1.jar
spring-framework-2.5.5/lib/log4j/log4j-1.2.15.jar
spring-framework-2.5.5/lib/slf4j/slf4j-api-1.5.0.jar
spring-framework-2.5.5/lib/slf4j/slf4j-log4j12-1.5.0.jar
spring-framework-2.5.5/lib/j2ee/*.jar
hibernate-annotations-3.4.0.GA/hibernate-annotations.jar
hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
hibernate-distribution-3.3.1.GA/hibernate3.jar
hibernate-distribution-3.3.1.GA/lib/required/javassist-3.4.GA.jar
hibernate-distribution-3.3.1.GA/lib/required/slf4j-api-1.5.2.jar
Because we will be using Oracle Stored Procedures, we will also need a database driver such as
oracle/product/10.2.0/db_1/jdbc/lib/ojdbc14.jar
Create Domain Objects
We can setup our domain using annotated Java. For these examples, we need one simple domain Object.
package spring.hibernate.oracle.stored.procedures.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = “AUTHOR”, schema = “MY_ORCL”)
public class Author implements java.io.Serializable {
private static final long serialVersionUID = 8676058601610931698L;
private int id;
private String firstName;
private String lastName;
@Id
@Column(name = “ID”, nullable = false)
public int getId() {
return this.id;
}
public void setId(final int id) {
this.id = id;
}
@Column(name = “FIRST_NAME”, nullable = false, length = 50)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
@Column(name = “LAST_NAME”, nullable = false, length = 50)
public String getLastName() {
return this.lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
}
Create a DAO
Now that we have a domain Object, we can create a DAO for a simple operation, such as looking up Authors by last name. Fortunately, Spring provides a convenient base class for DAO operations.
package spring.hibernate.oracle.stored.procedures.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import spring.hibernate.oracle.stored.procedures.domain.Author;
public class AuthorDAO extends HibernateDaoSupport {
@SuppressWarnings(“unchecked”)
public List findByLastNameUsingHQL(final String lastName) {
return getHibernateTemplate().find(“from Author author where author.lastName = ?”, lastName);
}
}
The Spring Application Context Configuration
The Spring applicationContext.xml can reside directly at the root of our src classpath, and it will contain information for configuring Spring to manage our Hibernate sessions, transactions and datasources, as well as our DAO.
相关文章推荐
- Oracle 中的JOIN
- Spring, Hibernate and Oracle Stored Procedures
- ORACLE SQL 行函数
- oracle故障代码
- ORACLE SQL WHERE和ORDER BY
- ORACLE SQL 基本操作和基本SELECT
- Oracle 数据库监听配置和服务
- Oracle EBS并发请求启用跟踪
- python操作oracle和mysql
- Oracle数据泵导入导出数据,建立表空
- Oracle之ROW_NUMBER() OVER函数
- oracle 数据库的一些操作和说明
- oracle字符串处理函数
- ORACLE锁机制深入理解
- oracle正则表达式regexp_like的用法详解
- Mybatis 连接oracle数据库配置文件
- ORACLE触发特定的解释
- Oracle like '%...%' 优化
- oracle 往表中插入多条数据(整理)
- Oracle常用コマンド((命令)(liunx)