如何保留一组主键标识符用于预加载引导数据
我们希望为所有表保留一组主键标识符(例如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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
TABLE
策略而不是IDENTITY
。来自 Hibernate 书:这是示例有更多解释。还有一个更复杂的设置初始值的示例。
您还可以尝试使用在
orm.xml
中定义的自定义序列生成器,如下所示:You could try the
TABLE
strategy instead ofIDENTITY
. From the Hibernate book: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:如果其他一切都失败,您始终可以编写自己的自定义 ID 生成器并在 DAO 的
create(EntityEntity)
方法中使用它。 ID 序列表可能类似于这可能意味着表
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 likeThis 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.