6

Getting started with NHibernate

How can I generate identity fields in nHibernate using Hilo algorithm?

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452

2 Answers2

7

use class="hilo":

<generator class="hilo">

example:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernate__MyClass" assembly="NHibernate__MyClass">
  <class name="MyClass" table="MyClass">
    <id name="Id" type="int" column="ID">
      <generator class="hilo">
    </id>
    <property name="Name">
      <column name="Name" not-null="true" />
    </property>
    <property name="Value">
      <column name="Value" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

I simplified:

<id name="Id">
  <column name="ID" sql-type="int" not-null="true"/>
  <generator class="hilo" />
</id>

to:

<id name="Id" type="int" column="ID">
    <generator class="hilo">
</id>

You could have a syntax error of some sort that is confusing NHibernate.
If you could provide more detail about the code that is executing before the failure or anything else you might think is important, that could speed the rate at which your problem is resolved.

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
  • I am using SQL Server. Should I create additional tables/columns for hilo? –  Jul 04 '09 at 12:22
  • I.e. does NHib need s additional tables/columns to work with hilo? –  Jul 04 '09 at 12:26
  • Could you provide the code where your doing the insert that fails, I think that might have something to do with the problem. The error message says at some point, your trying to insert a null id. – Mark Rogers Jul 04 '09 at 18:08
  • JMSA - Yes it does require an additional table [hibernate_unique_key] for the hilo algorithm but you don't have to create it manually if the sql account has create table privledges. – Steven Lyons Jul 09 '09 at 06:30
4

I haven't watched the screencasts yet. But Summer of nHibernate should help you.

I am sorry - I am not answering your original question.

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88