hibernate HiLo - 所有实体的一个表

时间:2012-03-07 13:05:13

标签: java sql hibernate hilo

我见过这个问题

NHibernate HiLo - one table for all entities

所以我读过

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

我尝试在hibernate上做。 hibernate不会检查属性hashmap中的“where”键,所以我尝试自己覆盖configure函数,但我无法读取或更新该值。

有一个现成的解决方案吗?我的代码如下。

@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")+"'"
             ;
}

myTableNameForHiloValues表的结构如下:

EntityName | NextHiValue

1 个答案:

答案 0 :(得分:1)

我已经设法做了我想要的,而不是每个表的行,但每个表有列。

它是这样的:在myHiloTable上,每个实体一列,我想用hilo生成id。所以它将是一个包含一行和多列的表。

myHiloTable:

entity1 | entity2 | entity3

,entity2的配置示例为:

<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>