9

我目前正在为我的课程使用 hilo 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,我做错了什么。

4

2 回答 2

2

我再次问了这个问题,但是在 nhuser 组中,请参阅此处以获取我得到的回复

于 2009-09-07T08:38:33.713 回答
1

我做到了,也许暂时有点脏,但无论如何:

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() };
        }

    }
于 2009-10-07T11:07:39.273 回答