-->
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: ID generator using increment and Oracle Schema
PostPosted: Wed Oct 14, 2009 9:16 am 
Newbie

Joined: Wed Oct 14, 2009 8:52 am
Posts: 2
I am using Hiberbnate 3.1.3
I have a mapping as below and when I try to insert a record into the TEST_TABLE, I get an Exception: 'Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator'
Code:
   
<class name="com.test.app.to.TestTable" table="TEST_TABLE" schema="TEST">
        <id name="testId" type="long">
            <column name="TEST_ID" precision="12" scale="0" />
            <generator class="increment"></generator>
        </id>
</class>

I have default schema set as below in the cfg.xml as I need to use tables from the OTHER_SCHEMA as well in my application.
Code:
<property name="hibernate.default_schema">OTHER_SCHEMA</property>

In the above case it seems to be a Hibernate Bug as a read using a TestTable object works fine and uses the 'TEST' schema correctly, but the '<generator class="increment"></generator>' does not use the 'TEST' schema but uses the default 'OTHER_SCHEMA' for getting the max ID. The query generated for max ID reads as below:
Code:
Hibernate: select max(TEST_ID) from OTHER_SCHEMA.TEST_TABLE

I am not able to specify a schema for the generator and it is not using the schema="TEST" attribute of the class which I would expect it to use.

How can this issue be resolved?


Top
 Profile  
 
 Post subject: Re: ID generator using increment and Oracle Schema
PostPosted: Wed Oct 14, 2009 3:14 pm 
Newbie

Joined: Wed Oct 01, 2008 8:42 pm
Posts: 12
Hi,
This doesn't answer your question, and I usually can't stand it when people assume they know what I'm doing but, I ran into some issues due to a misunderstanding of the increment strategy when I first started using hibernate, so I thought I'd share.

If I recall correctly, the increment strategy is not "multi-application" safe. It works by getting the max ID at startup and then keeps that number in memory and increments it as is needed, all in the applications memory. I ran into issues where I was using a separate application to access my database in addition to the app I was developing. Problems arose when I would try and insert a new record into the separate application and then use the app I was making to insert another one. Since my app was unaware of the other application, it simply tried to increment its counter and I got a primary key constraint violation.

Again, maybe you're well aware of this and I'm talking for no reason at all.

However, if this is news to you, a better alternative (I think) is to use sequences inside of oracle. Hibernate supports these fully, and I haven't had any issues with them thus far. However, this may or may not help you with the schema issue you are having.

-Tim


Top
 Profile  
 
 Post subject: Re: ID generator using increment and Oracle Schema
PostPosted: Wed Oct 14, 2009 5:43 pm 
Newbie

Joined: Wed Oct 14, 2009 8:52 am
Posts: 2
In my current application, I am not limited by the "multi-application" safety as it is a small standalone app.
Also, using the increment will help in cases where:
  • You don't have access to create database objects
  • Other systems insert data not using the sequence

The following answer helps resolve my issue:
http://stackoverflow.com/questions/1566263/hibernate-id-generator-using-increment-and-oracle-schema

Thanks for your insights into the working of the increment strategy and your suggestion on using sequence. The sequence worked with multiple schema as the I was able to specify the sequence param as <SCHEMA_NAME>.<SEQUENCE_NAME>. I will use it wherever possible.


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.