如何使用 Hibernate 从 0 而不是 1 开始自动增量?

How do I start autoIncrement from 0 instead of 1 with Hibernate?

请帮忙。 我有一个 table,它使用从 0 开始的外键值。 当我尝试使用休眠生成实体时,它总是从 1 开始自动递增。有没有办法强制它从 0 开始?
我的休眠主键目前是这样设置的:

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Long id;
SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.

This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.)

Reference

Reference 2

我同意西蒙的观点。 0 不是主键的好值。此外,据我所知,没有办法配置 Hibernate 来进行这种不良做法。但是如果你真的想坚持从零开始,你可以删除自动增量并制作: SELECT MAX(column_name) FROM table_name; 并解决它。但在我看来,作为开发人员,0 在处理数组等时是一个很好的值,但 PK 应始终以 1 开头。