Hibernate version: 3.1.3
We have a distributed system with operations in different locations. All of them use their local servers and database (MySQL). All these different systems need to synchronize their data with the central database every night. This means that the primary keys generation cannot be a simple identity strategy as it would result in conflicts. We are considering what would be the best option to take.
One of the strong candidates is providing each location with a unique id which would be prefixed to the id. Sounded good, so we got down to implementing it.. and immediately ran into problems.
I tried to create a custom IdentityGenerator which will do the job of gettingm the latest data and concatenating with its unique id - it somehow doesnt work :(
Code:
public class PrefixedNumericIdentityGenerator extends IdentityGenerator {
...
....
public Serializable getResult(SessionImplementor session, ResultSet rs, Object object, PostInsertIdentityPersister persister)
throws SQLException
{
String result = rs.getString(1);
String strVal = prefix + result;
if ( idClass==Long.class ) {
Long longVal = new Long(strVal);
return longVal;
}
else if ( idClass==Integer.class ) {
Integer intVal = new Integer(strVal);
return intVal;
}
else if ( idClass==Short.class ) {
Short shortVal = new Short(strVal);
return shortVal;
}
else
throw new IdentifierGenerationException("this id generator generates long, integer or short ");
}
Config :
Code:
<class name="Client" table="Client" >
<id name="id" type="integer" unsaved-value="null" >
<column name="id" not-null="true"/>
<generator class="com.test.PrefixedNumericIdentityGenerator" >
<param name="prefix">2005</param>
</generator>
</id>
x
Any inputs on how we could bring this off ?
Thanks[/code]