Using Oracle Sequence in Spring -OracleSequenceMaxValueIncrementer

 

                            NOTE: Source Code available for download at the bottom of the page

What are we going to learn?
    In this article we will see how to use Oracle Sequence Incrementer in Spring Context. 

What do you need ?
  • Maven
  • JDK 1.5 or higher
  • eclipse 
  • Spring 3.2
  • Tomcat
What's the overall design?
   We will create DAO method for Student to insert a student record. We will use oracle sequence to generate the stude id field. We will also see how to create the     sequence incrementer in the Spring context.

Let's get started !

 Create a maven project of artifact "maven-archetype-quickstart" as shown here. Remember to select the correct artifact "maven-archetype-quickstart" as shown below.

artificat selection


NOTE : Oracle jar is not available in maven due to licensing. In order to use oracle in maven project, the jar should be installed in local repository as shown in the link "How to add External jars in Maven". 

Steps Involved:
 Step 1: Update pom file for required dependencies.
 Step 2: Create spring application context.
 Step 3: Create Model and DAO methods to insert a Student record.
 Step 4: Write main class to insert student record.

Step 1: Update pom file for required dependencies.
  
 Update pom file for required dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.javavision</groupId>
	<artifactId>SpringStandAlone</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringStandAlone</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0</version>
		</dependency>


	</dependencies>
</project>


Step 2: Create spring application context.

application-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context     
     http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:component-scan
		base-package="com.javavision.SpringOracleSequenceIncrementer" />

	<bean id="studentDao"
		class="com.javavision.SpringOracleSequenceIncrementer.dao.StudentDaoImpl">
		<property name="jdbcTemplate" ref="myJdbcTemplate" />
	</bean>

	<bean id="myJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@hostname:port-number:oracle-sid" />
		<property name="username" value="username" />
		<property name="password" value="password" />
	</bean>

	<bean id="studentSequenceIncrementer"
		class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer">
		<property name="dataSource" ref="dataSource" />
		<property name="incrementerName" value="STUDENT_SEQ" />
	</bean>
</beans>

The bean "studentSequenceIncrementer" will be an instance of 'OracleSequenceMaxValueIncrementer" which will use oracle sequence "STUDENT_SEQ" defined in the "dataSource". 



Step 3: Create Model and DAO methods to insert a Student record.

Student.java
package com.javavision.SpringOracleSequenceIncrementer.model;

public class Student {
	private Long id;
	private String firstName;
	private String lastName;

	public Student(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}




StudentDao - Interface
package com.javavision.SpringOracleSequenceIncrementer.dao;

import com.javavision.SpringOracleSequenceIncrementer.model.Student;

public interface StudentDao {
	public int insertStudent(Student student);
}


StudentDaoImpl - StudentDao Implementation
package com.javavision.SpringOracleSequenceIncrementer.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;

import com.javavision.SpringOracleSequenceIncrementer.model.Student;

public class StudentDaoImpl extends JdbcDaoSupport implements StudentDao {

	@Autowired
	private OracleSequenceMaxValueIncrementer studentSequenceIncrementer;

	public int insertStudent(Student student) {
		String INSERT_STUDENT = "INSERT INTO STUDENT (STUDENTID, FIRST_NAME, LAST_NAME ) VALUES  (?, ?, ? )";

		int noOfrowsUpdated = getJdbcTemplate().update(INSERT_STUDENT,
				new Object[] { studentSequenceIncrementer.nextLongValue(), student.getFirstName(), student.getLastName() });
		return noOfrowsUpdated;

	}

}



Project Structure:

Spring Oracle Sequence Incrementer
Source Code : SpringOracleSequenceIncrementer.rar
Web Analytics