简体   繁体   中英

Spring + Hibernate: configuring PK generator?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM