一篇英文博文,写的是利用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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

Spring, Hibernate and Oracle Stored Procedures的更多相关文章

  1. [转]How to: Execute Oracle Stored Procedures Returning RefCursors

    本文转自:http://www.telerik.com/help/openaccess-orm/openaccess-tasks-oracle-execute-sp-result-set.html I ...

  2. [转]Oracle Stored Procedures Hello World Examples

    本文转自:http://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/ List of quick examp ...

  3. SSH面试题(struts2+Spring+hibernate)

    struts2 + Spring +hibernate Hibernate工作原理及为什么要用?   原理:   1.读取并解析配置文件   2.读取并解析映射信息,创建SessionFactory ...

  4. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  5. SSH框架简化(struts2+spring+hibernate)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...

  6. Spring/Hibernate 应用性能优化的7种方法

    对于大多数典型的 Spring/Hibernate 企业应用而言,其性能表现几乎完全依赖于持久层的性能.此篇文章中将介绍如何确认应用是否受数据库约束,同时介绍七种常用的提高应用性能的速成法.本文系 O ...

  7. Performance Tuning of Spring/Hibernate Applications---reference

    http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise appli ...

  8. Spring / Hibernate 应用性能调优

    来源:ImportNew - 陈晓舜 对大部分典型的Spring/Hibernate企业应用来说,应用的性能大部分由持久层的性能决定. 这篇文章会重温一下怎么去确认我们的应用是否是”数据库依赖(dat ...

  9. Spring+Hibernate实现动态SessionFactory切换

    场景: 1)系统有多个数据库 2)且数据库类型也不尽相同 3)现在应用根据某些条件路由到具体的数据库 4)且在spring+hibernate框架下,支持依赖注入 已有实现,spring动态数据源,但 ...

随机推荐

  1. django 异步任务实现及Celery beat实现定时/轮询任务

    Celery定时任务 requirements celery==3.1.25 异步任务 django-celery==3.2.2 定时任务管理包 redis==2.10.6 django-redis- ...

  2. ARDUINO使用GPRS发送GPS数据到OneNet测试

    功能: 测试把固定的GPS数据发送到OneNet平台 调试途中碰到的问题 ARDUINO不支持sprintf的double打印,只能转换为char字符串然后再%s打印 #include <Tim ...

  3. 通过GPRS将GPS数据上传到OneNet流程

    AT OK AT+CGCLASS="B" OK AT+CGDCONT=1,"IP","CMNET" OK AT+CGATT=1 OK AT+ ...

  4. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  5. HackerRank - beautiful-binary-string 【字符串】

    题意 给出一个 N 位的 01 串 然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0) 然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 ...

  6. 通过套接字(socket)和UDP协议实现网络通信

    UDP---用户数据报协议,是一个简单的面向数据报的运输层协议.(无连接.封包.大小限制.速度快). 一.UDP协议的特点: 将数据及源和目的地封装成数据包中,不需要建立连接. 每个数据报的大小限制在 ...

  7. Mysql5.5 InnoDB存储引擎配置和优化

    环境为CentOS系统,1G内存,Mysql5.5.30.在/etc/my.cnf内添加: 复制代码代码如下: skip-external-lockingskip-name-resolvemax_co ...

  8. wp8使用现有sqlite数据库

    就是把现有文件转移到隔离空间即可 代码如下 private async void CopyDB()        {            StorageFile fage = await Appli ...

  9. getline()函数详解 (2013-03-26 17:19:58)

     学习C++的同学可能都会遇到一个getline()函数,譬如在C++premer中,标准string类型第二小节就是“用getline读取整行文本”.书上给的程序如下: int main() {   ...

  10. 2017各银行贷款利息表及P2P平台贷款利率比较

    银行贷款利息是多少?2017各银行贷款利息表及P2P平台贷款利率比较 发表时间: 2017-02-17 作者: 一.2017央行贷款基准率 各个银行的贷款利率是以中国银行的人民币贷款基准率为标准进行上 ...