kommradHomer
kommradHomer

Reputation: 4212

hibernate HiLo - one table for all entities

I've seen this question

NHibernate HiLo - one table for all entities

and so I've read

http://daniel.wertheim.se/2011/03/08/nhibernate-custom-id-generator/

and i tried to do it on hibernate . hibernate does not check the "where" key in the properties hashmap so i try to override the configure function myself but i cant read or update the value.

is there a ready solution for this ? my code is below.

@Override
public void configure(Type type, Properties params, Dialect dialect)
{
    tableName="myTableNameForHiloValues"; //hardcoded
    columnName="NextHiValue";//the column that holds the next hi value
    String schemaName = params.getProperty(SCHEMA);
    String catalogName = params.getProperty(CATALOG);

    tableName = Table.qualify( catalogName, schemaName, tableName );

    query = "select " + columnName +
            " from " //+ dialect.appendLockHint(LockMode.UPGRADE, tableName) +      
              dialect.getForUpdateString() +
            " where EntityName='"+params.getProperty("target_table")+"'"
              //here i give the entity that i want to use
            ;

    update = "update " + tableName +
             " set " + columnName + " = ? "+
             " where " + columnName + " = ?"+
             " and EntityName='"+params.getProperty("target_table")+"'"
             ;
}

the structure of the myTableNameForHiloValues table is like :

EntityName | NextHiValue

Upvotes: 0

Views: 1367

Answers (1)

kommradHomer
kommradHomer

Reputation: 4212

I've managed to do what i want , not with row per table , but with column per table.

It goes like this : on my myHiloTable , one column per entity that i want to use hilo for generating ids. so it will be a table with one row and many columns.

myHiloTable :

entity1 | entity2 | entity3

and the configuration example for entity2 is :

<id name="whateverYourIdNameIs" type="java.lang.Integer">
        <column name="whateverYourColumnNameIs" />           
        <generator class="hilo">
            <param name="table">myHiloTable</param> //tablename is same for all entities
            <param name="column">entity2</param>  //column name changes for entity
        </generator>
</id>

Upvotes: 1

Related Questions