views:

878

answers:

4

I'm using hibernate to persist an entity to my database. For the moment it's a derby DB, but I will be moving it over to an oracle DB soon.

in my classname.hbm.xml I have the id defined as such:

<id name="id" type="long">
      <column name="ID"/>
      <generator class="increment"/>
</id>

How do I set the starting value for the id? I'd like it to start at something like 10000 rather than 1.

+1  A: 

"increment" isn't really a good idea for ID generation.

If you're moving to Oracle, then you'll use a sequence to generate IDs, and the sequence is controlled by oracle, so you can make it start with whichever value you want.

Derby doesn't support sequences, though - in fact, I'm not sure if Hibernate officially supports Derby at all.

skaffman
+2  A: 

I don't think you can, it appears that IncrementGenerator works based off the highest primary key so unless you fix your primary keys (which I wouldn't recommend) you're stuck with that functionality.

Would recommend a different strategy, it isn't safe in a cluster of machines either... Look at the Hibernate docs and maybe the enhanced identifier generators:

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-declaration-id-enhanced

Jon
+1  A: 

The increment generator typically is not the best choice. See the Hibernate documentation on advanced id generators for some hints.

If you are outside a cluster, no other applications are using the table, increment might give you the advantage of consecutive identifiers.

mkneissl
+1  A: 

Generator type increment looks up the maximum numeric primary key value in the table and increments it by one.

Generator type native uses a sequence when the underlying database used is Oracle. You can easily augment the value of the sequence to an arbitrary start value.

Ruben