-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cannot get sequences working (Oracle 10g)
PostPosted: Mon May 30, 2011 12:43 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
Hello,

First time Hibernate user having trouble getting sequences working.

I use annotations to configure my POJOs, and here is an example of a place I specify my sequence:

Code:
   @SequenceGenerator(name = "generator", sequenceName = "ADDR_SEQ")
   @Id
   @GeneratedValue(strategy = SEQUENCE, generator = "generator")
   @Column(name = "ADDR_ID", nullable = false, precision = 18, scale = 0)
   public Long getAddrId() {
      return this.addrId;
   }


I can see in my debug output that Hibernate actually does a call to this sequence, and I have had an issue where it threw an Oracle error because I initially forgot to create the sequence specified, and the error went away after I created it.

Code:
Hibernate:
    select
        ADDR_SEQ.nextval
    from
        dual


However, it seems Hibernate is not even using the sequence number it selects, because I am seeing it use numbers such as:

Quote:
4296500, 4296550, 4296600, 4296650


Those are exactly the last 4 records I inserted.

Meanwhile, if I do this:

Code:
select addr_seq.nextval from dual


I get the result:

Quote:
85934


So something is definitely wrong! I just dont know what it is....

What am I missing?

I specified my hibernate with following properties:

Code:
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.transaction.factory=org.hibernate.transaction.JDBCTransactionFactory


The database is setup from a JNDI datasource.

When trying to research this issue I came across some talk about a entityManager in Spring config files and I thought oh maybe I need to add that but then it started to get confusing so I need to ask do I really need to do that? In my Hibernate book it didnt mention anything like that....

I got to the point where it complained about a missing persistence.xml file and when I tried creating some from examples I am finding I'd have to repeat properties I set on Hibernate in here such as dialect and datasource so I really feel like it might be going down the wrong path.


Top
 Profile  
 
 Post subject: Re: Cannot get sequences working (Oracle 10g)
PostPosted: Mon May 30, 2011 5:53 pm 
Newbie

Joined: Thu May 19, 2011 12:59 am
Posts: 13
I found a solution to my problem. I don't understand why it made such a difference, but I am at least satisfied it works.

I modified my annotation as follows:

Code:
@SequenceGenerator(name = "generator", sequenceName="ADDR_SEQ", allocationSize = 1, initialValue= 1)


Would like to know why this made it work "properly" instead of the default behavior working like it's supposed to. Now with those attributes added, Hibernate is taking the actual next value of thew sequence instead of some large arbitrary number and incrementing 1 by 1 instead of by 50.


Top
 Profile  
 
 Post subject: Re: Cannot get sequences working (Oracle 10g)
PostPosted: Mon May 30, 2011 6:20 pm 
Newbie

Joined: Mon May 30, 2011 4:58 pm
Posts: 3
i somewhere read that hibernate uses the allocationSize * current sequence value.
That results in 4296700 for your next insert, when you consider that the standard batch size is 50.

Hibernate does that so taht it does not have to select the next sequence value on every insert.
Your "fix" is the same one i used on similar problems. Reducing the allocationSize to 1 enforces hibernate to call the sequence every time.
That is fine for me, but may result in less performance during inserts...

That of course does not explain why you encounter the error whith the default value...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.