Thanks to ccanning, this issue has been resolved.
Attached below is the code and the mapping file to achieve this :
Oracle Sequence Generator Class
Code:
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
import com.hibernate.model.vo.OracleSequence;
public class OracleSequenceGeneratorType implements UserType, ParameterizedType, Serializable {
private String sequenceName;
Logger _log = Logger.getLogger(OracleSequenceGeneratorType.class);
public Object assemble(Serializable cached, Object owner) throws HibernateException {
_log.error("OracleSequenceGeneratorType.assemble: Object cached [" + cached.getClass().getName() + "]" );
_log.error("OracleSequenceGeneratorType.assemble: Object owner [" + owner.getClass().getName() + "]" );
return cached;
}
public Object deepCopy(Object value) throws HibernateException {
_log.error("OracleSequenceGeneratorType.deepCopy: Object [" + value.getClass().getName() + "]" );
return value;
}
public Serializable disassemble(Object value) throws HibernateException {
_log.error("OracleSequenceGeneratorType.disassemble: Object Type [" + value.getClass().getName() + "]" );
return (Serializable) value;
}
public boolean equals(Object x, Object y) throws HibernateException {
_log.error("OracleSequenceGeneratorType.equals: Object [" + x.getClass().getName() + "]" );
if (x==y) return true;
if (x==null || y==null) return false;
return x.equals(y);
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean isMutable() {
return false;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
_log.error("OracleSequenceGeneratorType.nullSafeGet" );
OracleSequence result = new OracleSequence();
result.setSequence(new Long(rs.getLong(names[0])));
return result;
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
_log.error("OracleSequenceGeneratorType.nullSafeSet" );
OracleSequence sequence;
Long nextValue;
_log.error("OracleSequenceGeneratorType.nullSafeSet: binding passed in variable : [" + value + "] at index [" + index + "]");
//cast the value to the oracle sequence (should not be null since constructed in owning object
sequence = (OracleSequence)value;
//check to see if we have already set the sequence (could be update, etc... also use this to generate new sequence
if (sequence.getSequence() == null) {
//get the next sequence value
nextValue = getNextValue(st);
//set it on the internal/owning sequence object
sequence.setSequence(nextValue);
}
//set the value on the prepared statement
st.setLong(index, sequence.getSequence().longValue());
_log.error("OracleSequenceGeneratorType.nullSafeSet: Retrieved Sequence Value = [" + sequence.getSequence() + "]");
}
private Long getNextValue(PreparedStatement st) throws SQLException {
Connection conn = st.getConnection();
Statement sta;
ResultSet rs;
Long nextValue;
String sql = "select " + sequenceName + ".nextval from dual";
sta = conn.createStatement();
rs = sta.executeQuery(sql);
rs.next();
nextValue = new Long(rs.getLong(1));
rs.close();
sta.close();
return nextValue;
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
_log.error("OracleSequenceGeneratorType.replace: Object Type Original [" + original.getClass().getName() + "]" );
_log.error("OracleSequenceGeneratorType.replace: Object Type target [" + target.getClass().getName() + "]" );
_log.error("OracleSequenceGeneratorType.replace: Object Type owner [" + owner.getClass().getName() + "]" );
return original;
}
public Class returnedClass() {
_log.error("OracleSequenceGeneratorType.returnedClass" );
return OracleSequence.class;
}
public int[] sqlTypes() {
return new int[] {Types.INTEGER};
}
public void setParameterValues(Properties parameters) {
sequenceName= (String) parameters.get("sequence-name");
_log.error("Recevied Parameter Name : 'sequence-name' = [" + sequenceName + "]");
}
}
Oracle Sequence Class :Code:
public class OracleSequence {
Long sequence;
public Long getSequence() {
return sequence;
}
public void setSequence(Long sequence) {
this.sequence = sequence;
}
}
Mapping File for Customer table :Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 22, 2007 5:06:53 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
<class name="com.model.vo.MPCustomerVO" table="MP_CUSTOMER">
<id name="UUID" type="string">
<column name="CUSTOMER_ID" length="32" />
<generator class="uuid" />
</id>
<timestamp name="lastUpdated" column="LAST_UPDATED" generated="never"/>
<property name="identity" column="CUSTOMER_IID" update="false" insert="true" unique="true" not-null="true">
<type name="com.dao.hibernate.OracleSequenceGeneratorType">
<param name="sequence-name">MP_CUSTOMER_SEQ</param>
</type>
</property>
<property name="customerName" type="string">
<column name="CUSTOMER_NAME" length="200" />
</property>
<property name="customerAddress1" type="string">
<column name="CUSTOMER_ADDRESS1" length="200" />
</property>
<property name="customerAddress2" type="string">
<column name="CUSTOMER_ADDRESS2" length="200" />
</property>
<!-- set name="orderList" inverse="true" cascade="all">
</class>
</hibernate-mapping>