I am using Hibernate 2.1 with Oracle 8.1.7.
I have an class in my model as :
Code:
package tavant.platform.persistence.hibernate.model;
public class Note
{
// PK field
private Long id;
// Attribute fields
private byte[] notes;
private Double type;
private Long versionNumber;
public Long getId()
{
return id;
}
private void setId(Long anId)
{
id = anId;
}
public byte[] getNotes()
{
return notes;
}
public void setNotes(byte[] notes)
{
this.notes = notes;
}
public Double getType()
{
return type;
}
public void setType(Double type)
{
this.type = type;
}
public Long getVersionNumber()
{
return versionNumber;
}
public void setVersionNumber(Long versionNumber)
{
this.versionNumber = versionNumber;
}
}
The mapping file for the above class is as :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="tavant.platform.persistence.hibernate.model">
<class name="Note" table="NOTES" proxy="Note">
<id name="id" column="LOAN_NOTES_ID" type="long">
<generator class="sequence">
<param name="sequence">LOAN_NOTES_SEQ</param>
</generator>
</id>
<version column="VERSION_NUMBER" name="versionNumber" type="long" unsaved-value="negative"/>
<property name="notes" column="LOAN_NOTES" type="binary"/>
<property name="type" column="TYPE" type="double"/>
</class>
</hibernate-mapping>
I have some test code as :
Code:
public void testSequences() throws HibernateException
{
Note note1 = new Note();
note1.setNotes( ("Created at " + new Date()).getBytes() );
note1.setType(new Double(1.11D));
Note note2 = new Note();
note2.setNotes( ("Created at " + new Date()).getBytes() );
note2.setType(new Double(1.11D));
Session session = getSessionFactory().openSession();
Transaction transaction1 = session.beginTransaction();
session.save(note1);
transaction1.commit();
Transaction transaction2 = session.beginTransaction();
session.save(note2);
transaction2.commit();
System.out.println("Note1.id " + note1.getId());
System.out.println("Note2.id " + note2.getId());
session.close();
}
The SQL fired by the above test is :
Code:
Hibernate: select LOAN_NOTES_SEQ.nextval from dual
Hibernate: insert into NOTES (VERSION_NUMBER, LOAN_NOTES, TYPE, LOAN_NOTES_ID) values (?, ?, ?, ?)
Hibernate: select LOAN_NOTES_SEQ.nextval from dual
Hibernate: insert into NOTES (VERSION_NUMBER, LOAN_NOTES, TYPE, LOAN_NOTES_ID) values (?, ?, ?, ?)
Note1.id 1950
Note2.id 2000
The increment size of my ORACLE sequence is 50. I would like the Hibernate Session to fetch the sequence only once every 50 inserts. In my above experiment, it is going to the DB everytime for a NEXTVAL of the SEQUENCE for
every insert.
Please advise me how to setup such a behaviour.
Thanks,
Binil