-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Caching ORACLE sequences
PostPosted: Wed Jul 21, 2004 7:46 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 7:59 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Just a quick question - what exactly do you want to happen?

If you configure a sequence to be used to generate the IDs of the Note, then what else can it do except select the next valid value out of the sequence?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 8:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
check the sequence hilo generator

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 8:35 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
sdknott wrote:
Just a quick question - what exactly do you want to happen?

If you configure a sequence to be used to generate the IDs of the Note, then what else can it do except select the next valid value out of the sequence?


I expect to be able to specify the INCREMENT size of the SEQUENCE. And if I specify N as the INCREMENT size, I want a Hibernate Session to fetch NEXTVAL on the SEQUENCE only once in N inserts.

Hope that clarifies it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 9:01 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
emmanuel wrote:
check the sequence hilo generator


Thanks, that seems to have worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.