0

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

Community
  • 1
  • 1
kommradHomer
  • 4,127
  • 5
  • 51
  • 68
  • 1
    why don't you use one high value for all tables? makes some things easier – Firo Mar 07 '12 at 15:12
  • then the generated id would be unique in all the tables that share it right ? I think i just try to keep it unique to every single table so i can increase the "max_lo" value for performance. – kommradHomer Mar 07 '12 at 15:18
  • I've used highvalues for all tables in a production app which saves some roundtrips because i can have a big low value and there is only one roundtrip for a highvalue for a complete object graph (slightly better performance than multiple high values). And since my ids are unique globally i could implement locking and caching using only ids. I'm not sure if i can run out of ids with bigint that fast. – Firo Mar 08 '12 at 11:01
  • and equals is a lot easier when the id is uniqe over all entities – Firo Mar 08 '12 at 17:07
  • i still will go for using a hi value for every table that i put around 1 million entities in one session. so that i can convince the team that we have 8446 quadrillion id's available. and i got the problem solved btw , im using a column for every table i want to involve. – kommradHomer Mar 09 '12 at 07:09
  • would be nice if you post your solution as answer and accept it so others can benefit from it. – Firo Mar 09 '12 at 09:28
  • just so tired of answering my own questions . i ask myself "did i not try hard enough before asking?" , but i do really. anyway , i'll post yes , i guess comments aren't indexed as good as answers. – kommradHomer Mar 09 '12 at 10:28
  • 1
    maybe nobody could answer your question "is there a ready solution for this ?" even if a lot of people could have fixed the code. – Firo Mar 09 '12 at 10:44
  • that's such a good point mate. – kommradHomer Mar 09 '12 at 10:46

1 Answers1

1

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>
kommradHomer
  • 4,127
  • 5
  • 51
  • 68