Search code examples
nhibernatecomposite-id

NHibernate 3.3: composite-id with a key-property generated?


I have read that this mapping is not possible in NHibernate 3.3:

<class name="Digital" table="DIGITALS">
    <composite-id>
      <key-many-to-one name="Person" class="Person" column="PERSONID" />
      <key-property name="Id" column="ID">
        **<generator class="increment"/>**
      <key-property/>
    </composite-id>
    <property name="Nombre" column="NOMBRE" />

Basically I need a composite-id's property to be calculated automatically by NH.

Maybe exists a technique to get something similar?

Thanks in advance.


Solution

  • you have to implement it yourself since CompositeIds are always generatedby assigned for NH

    class Digital
    {
        private static long number = 0;
    
        private static long NextNumber()
        {
            return Interlocked.Increment(ref number);
        }
    
        public Digital()
        {
            Id = NextNumber();
        }
    }