java 如何在休眠中为自动增量提供起始值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14526312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:32:57  来源:igfitidea点击:

How to give starting value to auto increment in hibernate

javaspringhibernate

提问by user57358

I am new to hibernate. How do I specify in Hibernate the starting value of my ID (say 100000) and auto increment the ID starting at this value in my code. Any help on this is appreciated.

我是休眠的新手。我如何在 Hibernate 中指定我的 ID 的起始值(比如 100000)并在我的代码中从这个值开始自动增加 ID。对此的任何帮助表示赞赏。

@Id
@Column (name = "ID")
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
private Integer ID;

采纳答案by Sotirios Delimanolis

Try this: http://www.java2s.com/Code/Java/JPA/SetInitialValueOfTableGenerator.htm

试试这个:http: //www.java2s.com/Code/Java/JPA/SetInitialValueOfTableGenerator.htm

JPA has a @TableGenerator annotation in which you can set an initial value.

JPA 有一个 @TableGenerator 注释,您可以在其中设置初始值。

回答by user57358

I found a way around this. I manually entered a row in the database with ID 1000000 and used the code mentioned in my question. The next time a add a record to this table, it takes 1000000 as the last value and starts incrementing from this value. Thanks for your help.

我找到了解决这个问题的方法。我在数据库中手动输入了 ID 为 1000000 的一行,并使用了我的问题中提到的代码。下一次向该表中添加记录时,它会将 1000000 作为最后一个值并从该值开始递增。谢谢你的帮助。

回答by Ayman Al-Absi

if first required ID is 1000000, then use @TableGenerator but if first required ID is 1000001, then use @SequenceGenerator.

如果第一个需要的 ID 是 1000000,那么使用@TableGenerator 但如果第一个需要的 ID 是 1000001,那么使用 @SequenceGenerator。

This article explains the difference in a very clear way:

这篇文章以非常清晰的方式解释了差异:

http://www.objectdb.com/java/jpa/entity/generated

http://www.objectdb.com/java/jpa/entity/generated