如何保留一组主键标识符用于预加载引导数据

发布于 2024-08-27 21:13:39 字数 433 浏览 4 评论 0原文

我们希望为所有表保留一组主键标识符(例如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 工具驱动的解决方案。

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

We would like to reserve a set of primary key identifiers for all tables (e.g. 1-1000) so that we can bootstrap the system with pre-loaded system data.

All our JPA entity classes have the following definition for the primary key.

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

is there a way to tell the database that increments should start happening from 1000 (i.e. customer specific data will start from 1000 onwards). We support (h2, mysql, postgres) in our environment and I would prefer a solution which can be driven via JPA and reverse engineering DDL tools from Hibernate.

Let me know if this is the correct approach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-09-03 21:13:39

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

很像 Hibernate 的 hilo 策略,TABLE
依赖于保存最后生成的数据库表
整数主键值,并且每个
生成器映射到该表中的一行。
每行有两列:pkColumnName
valueColumnNamepkColumnValue 将每一行分配给一个特定的生成器,
值列保存最后一个
检索到主键。持久化提供者
最多分配 allocationSize 个整数
在每个回合中。

这是示例有更多解释。还有一个更复杂的设置初始值的示例

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

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

这声明一个名为 MY_SEQUENCE 的数据库序列,具有初始值
123 可以用作数据库标识符生成的源,并且持久性
引擎每次需要标识符时应该获取 20 个值。 (笔记,
不过,在撰写本文时,Hibernate Annotations 忽略了 initialValue 设置。)

要将此标识符生成器应用于特定实体,请使用其名称:

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

You could try the TABLE strategy instead of IDENTITY. From the Hibernate book:

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.

Here is an example with more explanation. And a more complex example for setting the initial value.

You could also try using a custom sequence generator, defined in your orm.xml, like this:

<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;
}
单调的奢华 2024-09-03 21:13:39

如果其他一切都失败,您始终可以编写自己的自定义 ID 生成器并在 DAO 的 create(EntityEntity) 方法中使用它。 ID 序列表可能类似于

-------------------------------------------------------------
| sequence_name | initial_value | current_value | increment |
-------------------------------------------------------------
|           foo |          1001 |          2000 |       100 |

这可能意味着表 foo 的 ID 从 1001 开始,并以 100 递增(因此您不必为每个新表插入调用 DB)。

这就是我们正在使用的,没有太多问题。

If everything else fails, you can always write your own custom ID generator and use it in your DAO's create(Entity entity) method. The ID sequence table could be something like

-------------------------------------------------------------
| sequence_name | initial_value | current_value | increment |
-------------------------------------------------------------
|           foo |          1001 |          2000 |       100 |

This could mean that IDs for table foo start at 1001 and are incremented by 100 (so you don't have to call DB for each new table insert).

That's what we're using without much problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文