Hibernate increment starting number

一个人想着一个人 提交于 2020-01-04 06:18:09

问题


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:


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




回答2:


"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 support sequences as of 10.6, though you need Hibernate 3.6 to get it to work..




回答3:


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.



来源:https://stackoverflow.com/questions/1296826/hibernate-increment-starting-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!