users@glassfish.java.net

First allocation of sequence data doesn't work

From: <glassfish_at_javadesktop.org>
Date: Tue, 23 Oct 2007 01:26:34 PDT

I have the following Sequence-definition in my entity:

@Entity
public class Exam {

        @Id
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "examSeq")
        @SequenceGenerator(name="examSeq",sequenceName="EXAM_SEQ", allocationSize=5)
        @Column(name="id")
        private int examId;
//...
}

With the following Oracle SEQ:
CREATE SEQUENCE EXAM_SEQ START WITH 4 INCREMENT BY 5;
(the DB contains three rows in the EXAM-table with PK 1-3)

Client code:
                EntityTransaction tx = em.getTransaction();
                tx.begin();
                Subject subject = em.find(Subject.class, "1");
                Exam exam = new Exam(new Date(), subject);
                Exam exam2 = new Exam(new Date(), subject);
                em.persist(exam);
                em.persist(exam2);
                tx.commit();

On my first attempt to insert two exam-objects it fails, as it tries to insert with PK=1 and PK=2

This is what I see in the log:
[TopLink Fine]: 2007.10.23 10:03:09.546--ServerSession(28890871)--Connection(11143287)--Thread(Thread[main,5,main])--SELECT EXAM_SEQ.NEXTVAL FROM DUAL
[TopLink Finest]: 2007.10.23 10:03:09.625--ServerSession(28890871)--Thread(Thread[main,5,main])--sequencing preallocation for EXAM_SEQ: objects: 5 , first: 0, last: 4
[TopLink Finest]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--assign sequence to the object (0 -> no.tutorial.jpa.university.Exam_at_7124af)
[TopLink Finest]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--Execute query DoesExistQuery()
[TopLink Finest]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--PERSIST operation called on: no.tutorial.jpa.university.Exam_at_125d61e.
[TopLink Finest]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--assign sequence to the object (1 -> no.tutorial.jpa.university.Exam_at_125d61e)
[TopLink Finer]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--begin unit of work commit
[TopLink Finest]: 2007.10.23 10:03:09.625--UnitOfWork(856873)--Thread(Thread[main,5,main])--assign sequence to the object (2 -> no.tutorial.jpa.university.Exam_at_7124af)

Why is it getting 1 and 2?? and not 4 and 5?? Bug?
On next execution of the client code I get PK= 5 & 6.

CAlling: SELECT EXAM_SEQ.NEXTVAL FROM DUAL after 2nd execution of unit-test gives me 14.

How does this work? I don't udnerstand why it's using 1 and 2 on first execution? Where is it getting these values from? It's also strange that it uses 4 and 5 on next exec, as a second nextval on exam_seq should return 9.
[Message sent by forum member 'magott' (magott)]

http://forums.java.net/jive/thread.jspa?messageID=241619