NHibernate的希洛 - 每个实体和希洛渔获新列(NHibernate HiLo - new

2019-08-02 23:14发布

我目前使用我的班希洛ID生成,但刚刚使用最低设置例如


<class name="ClassA">
    <id name="Id" column="id" unsaved-value="0">
      <generator class="hilo" />
    </id>
...

但要我真的是指定新列的NHibernate使用的foreach的实体,它提供一个最大的LO?


<class name="ClassA">
    <id name="Id" column="id" unsaved-value="0">
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">classA_nexthi</param>
        <param name="max_lo">20</param>
      </generator>
    </id>
...
<class name="ClassB">
    <id name="Id" column="id" unsaved-value="0">
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">classB_nexthi</param>
        <param name="max_lo">20</param>
      </generator>
    </id>
...

另外我注意到,当我做通过SchemaExport上面不会造成所有列 - 只有classB_nexthi,有没有别的我做错了什么。

Answer 1:

我再次问到这个问题,但nhusers组中,在这里看到的反应 ,我得到



Answer 2:

你是怎么解决这个问题? 我实现自己的idgenerator?

我没有和暂时,但无论如何也许有点脏:

public class TableHiLoGeneratorWithMultipleColumns : NHibernate.Id.TableHiLoGenerator
    {
        static HashSet<string> tables = new HashSet<string>();
        public override void Configure(IType type, IDictionary<string, string> parms, Dialect dialect)
        {
            string table;
            if (parms.ContainsKey("target_table"))
            {
                table = parms["target_table"];
                tables.Add(table);
                parms["column"] = string.Format("{0}_{1}", DefaultColumnName, table);
            }
            base.Configure(type, parms, dialect);
        }

        public override string[] SqlCreateStrings(Dialect dialect)
        {
            string createTableTemplate = "create table " + DefaultTableName + "({0})";

            string insertInitialValuesTemplate = "insert into " + DefaultTableName + "({0})" + " values ( {1} )";

            StringBuilder createTables = new StringBuilder();
            StringBuilder columns = new StringBuilder();
            StringBuilder inserts = new StringBuilder();
            StringBuilder initialInsert = new StringBuilder();
            StringBuilder insertsValues = new StringBuilder();
            foreach (string table in tables)
            {
                columns.AppendFormat("{0}_{1} {2},", DefaultColumnName, table, dialect.GetTypeName(columnSqlType));
                inserts.AppendFormat("{0}_{1},", DefaultColumnName, table);
                insertsValues.Append("1, ");
            }
            columns.Remove(columns.Length - 1, 1);
            inserts.Remove(inserts.Length - 1, 1);
            createTables.AppendFormat(createTableTemplate, columns);
            insertsValues.Remove(insertsValues.Length - 2, 2);
            initialInsert.AppendFormat(insertInitialValuesTemplate, inserts, insertsValues);

            return new[] { createTables.ToString(), initialInsert.ToString() };
        }

    }


文章来源: NHibernate HiLo - new column per entity and HiLo catches