views:

786

answers:

2

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!

+2  A: 

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.

Eric Rath
Yes, that's my current option. The problem with this solution is that it adds a deployment step.What I'd really like to do would be to have two different sequences, and to configure the mapping to point on sequence one or sequence two in the existing Spring conf files.
Sébastien RoccaSerra
A: 

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

insert VALUES('Sys1' || to_char(sequence.nextval), val1, val2, val3);

insert VALUES('Sys2' || to_char(sequence.nextval), val1, val2, val3);