春天 + Hibernate:如何拥有可配置的 PK 生成器?
我们使用 Spring + Hibernate 作为 Web 应用程序。
该Web应用程序将部署在两个不相关的生产站点上。 这两个生产站点将使用 Webapp 并行生成和使用人员数据。
我需要做的是确保这两个不相关的生产站点上生成的Person都具有不同的PK,以便我们可以随时合并这两个站点的Person数据。
对我施加的进一步限制是这些 PK 适合 Long
,因此我无法使用 UUID。
我想做的是将当前的休眠映射(将序列 S_PERSON
作为生成器:)更改
<hibernate-mapping default-cascade="save-update" auto-import="false">
<class name="com.some.domain.Person" abstract="true">
<id name="id">
<column name="PERSON_ID"/>
<generator class="sequence">
<param name="sequence">S_PERSON</param>
</generator>
</id>
...
</hibernate-mapping>
为可配置的内容,以便 PERSON_ID
从不同序列生成其 PK (可能是 S_PERSON_1
和 S_PERSON_2
),具体取决于部署站点的 Spring 配置文件。
当然,
<generator class="sequence">
<param name="sequence">${sequenceName}</param>
</generator>
这是行不通的,所以我必须找出其他东西......我想我的生成器应该指向一个可配置的bean,而该bean又指向一个序列或另一个序列,但我不知道如何做到这一点。 ..
有什么想法或解决方法吗?
谢谢!
We use Spring + Hibernate for a Webapp.
This Webapp will be deployed on two unrelated production sites. These two production sites will use the Webapp to generate and use Person data in parallel.
What I need to do, is to make sure that the Persons generated on these two unrelated production sites all have distinct PKs, so that we can merge the Person data from these two sites at any time.
A further constraint imposed to me is that these PKs fit in a Long
, so I can't use UUIDs.
What I'm trying to do is to change the current hibernate mapping, that has sequence S_PERSON
as generator:
<hibernate-mapping default-cascade="save-update" auto-import="false">
<class name="com.some.domain.Person" abstract="true">
<id name="id">
<column name="PERSON_ID"/>
<generator class="sequence">
<param name="sequence">S_PERSON</param>
</generator>
</id>
...
</hibernate-mapping>
into something configurable, so that PERSON_ID
have its PKs generated from different sequences (maybe S_PERSON_1
and S_PERSON_2
) depending on the deployment site's Spring configuration files.
Of course,
<generator class="sequence">
<param name="sequence">${sequenceName}</param>
</generator>
doesn't work, so I have to figure out something else... I guess my generator should point to a configurable bean that in turn points to a sequence or another, but I can't figure how to do that...
Any ideas or workaround?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在两个生产系统上使用序列,但定义不同:
生产系统 1:
创建序列 序列名称 开始于 1 增量为 2;
生产系统2:
创建序列 序列名称 开始于 2 增量 2;
第一个序列将仅生成奇数,第二个序列将仅生成偶数。
You could use sequences on both production systems, but define them differently:
Production System 1:
CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 2;
Production System 2:
CREATE SEQUENCE sequence_name START WITH 2 INCREMENT BY 2;
The first sequence will generate only odd numbers, the second only even numbers.
您基本上需要确保键的值属于不同的集合。 因此在 varchar 前面加上系统标识符。
注意:我还没有对此进行测试,但您应该能够为每个数据库定义一个正常序列
并执行类似的操作
You basically need to make sure the values for the keys fall into different sets. So prepending a varchar with a system identifier.
Note: I havent tested this but you should be able to define a normal sequence per database
and do something like