Sleep start number

I am using hibernate to persist an object to my database. This is a derby database for now, but I will be moving it to Oracle soon.

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

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

      

How do I set an initial value for id? I would like it to start with something like 10000, not 1.

+2


source to share


3 answers


I don't think this is possible, it looks like the IncrementGenerator works based on the highest primary key, so if you don't fix the primary keys (which I wouldn't recommend) you are stuck with this function.

Would recommend another strategy, it is insecure on a cluster of machines ... Have a look at the Hibernate docs and maybe the extended id generators:



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

+2


source


"increment" is actually not a good idea for ID generation.

If you go to Oracle, you will use a sequence to generate ids, and the sequence will be oracle controlled so you can start with what value you want.



Derby has support for sequences since 10.6, although you need Hibernate 3.6 to get it working .

+5


source


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

The native generator type uses consistency when the underlying database is used by Oracle. You can easily increase the sequence value to an arbitrary starting value.

+1


source







All Articles