gpt4 book ai didi

database - 如何保留一组主键标识用于预加载bootstrap数据

转载 作者:搜寻专家 更新时间:2023-10-30 23:23:43 24 4
gpt4 key购买 nike

我们想为所有表保留一组主键标识符(例如 1-1000),以便我们可以使用预加载的系统数据引导系统。

我们所有的 JPA 实体类都具有以下主键定义。

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
private Integer id;

有没有办法告诉数据库增量应该从 1000 开始发生(即客户特定数据将从 1000 开始)。我们在我们的环境中支持 (h2、mysql、postgres),我更喜欢可以通过 JPA 驱动的解决方案和来自 Hibernate 的逆向工程 DDL 工具。

让我知道这是否是正确的方法

最佳答案

您可以尝试使用 TABLE 策略而不是 IDENTITY。来自 Hibernate 书:

Much like Hibernate’s hilo strategy, TABLE relies on a database table that holds the last generated integer primary key value, and each generator is mapped to one row in this table. Each row has two columns: pkColumnName and valueColumnName. The pkColumnValue assigns each row to a particular generator, and the value column holds the last retrieved primary key. The persistence provider allocates up to allocationSize integers in each turn.

这里是 an example有更多的解释。还有一个更复杂的 example for setting the initial value .

您也可以尝试使用在 orm.xml 中定义的自定义序列生成器,如下所示:

<sequence-generator name="mySequenceGenerator"
sequence-name="MY_SEQUENCE"
initial-value="123"
allocation-size="20"/>

This declares that a database sequence named MY_SEQUENCE with an initial value of 123 can be used as a source for database identifier generation, and that the persistence engine should obtain 20 values every time it needs identifiers. (Note, though, that Hibernate Annotations, at the time of writing, ignores the initialValue setting.)

To apply this identifier generator for a particular entity, use its name:

@Entity
class name MyEntity {
@Id @GeneratedValue(generator = "mySequenceGenerator")
String id;
}

关于database - 如何保留一组主键标识用于预加载bootstrap数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2606182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com