NHibernate HiLo - new column per entity and HiLo catches
Asked Answered
B

2

9

I'm currently using the hilo id generator for my classes but have just been using the minimal of settings eg


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

But should I really be specifying a new column for NHibernate to use foreach entity and providing it with a max 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>
...

Also I've noticed that when I do the above the SchemaExport will not create all the columns - only classB_nexthi, is there something else I'm doing wrong.

Beckiebeckley answered 28/8, 2009 at 8:42 Comment(2)
Not sure on this one, so only a comment. Try keeping the same column name there and see what happens. I think NH will create a separate row for each table. Again - not sure...Lamrouex
do you mean for ClassA ... <generator class="hilo"><param name="column">aCol</para>.... and for ClassB <generator class="hilo"><param name="column">aCol</para>. If so wouldn't that mean I would now NOT have a column for the hi value per entity?Beckiebeckley
B
2

I asked this question again but in the nhusers group, see here for response i got

Beckiebeckley answered 7/9, 2009 at 8:38 Comment(0)
M
1

I did and maybe a little bit dirty for the moment but anyway:

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

    }
Massarelli answered 7/10, 2009 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.