oursgris
oursgris

Reputation: 2880

Abstraction of the column name with nhibernate

I have a problem with nhibernate mapping. I use the nhibernate 3.2 mapping by code with that code

namespace NhibernateLiaisonBase
{
    [Serializable]
    public class Cessions
    {
        #region Public Properties
        public virtual int IdCessions
        {
            get;
            set;
        }

        public virtual DateTime CessionsDate
        {
            get;
            set;
        }
    #endregion
    }

    public class CessionsMap : ClassMapping<Cessions>
    {
        public CessionsMap()
        {
            Id<int>(x => x.IdCessions, map => map.Column("SCES_ID"));
            Property<DateTime>(x => x.CessionsDate, map => map.Column("SCES_DATE"));
            Table("SCES_CESSIONS");
        }
   }
}

and

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping.ByCode.Conformist;

namespace NhibernateLiaisonBase
{
    [Serializable]
    public class CessionsLignes
    {
        #region Public Properties
        public virtual int IdCessionsLignes
        {
            get;
            set;
        }

    public virtual int QuantiteCessionsLignes
    {
        get;
        set;
    }

    public virtual Cessions cessions
    {
        get;
        set;
    }
    #endregion
}

    public class CessionsLignesMap : ClassMapping<CessionsLignes>
    {
        public CessionsLignesMap()
        {
            Id<int>(x => x.IdCessionsLignes, map =>
            {
                map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
            });
            Property<int>(x => x.QuantiteCessionsLignes, map => map.Column("SCESL_QTE"));
            ManyToOne<Cessions>(x => x.cessions, map => map.Column("SCESL_SCESID"));
            Table("SCESL_CESSIONSLIGNES");
        }
    }
}

When I use it rename the SCESL_SCESID column to IdCessionsLignes in the SCESL_CESSIONSLIGNES table. I generated the hbm.xml file, the result are : (first

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace="NhibernateLiaisonBase" assembly="NhibernateLiaisonBase" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Cessions" table="SCES_CESSIONS">
    <id name="IdCessions" column="SCES_ID" type="Int32" />
    <property name="CessionsDate" column="SCES_DATE" />
  </class>
</hibernate-mapping>

and

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace="NhibernateLiaisonBase" assembly="NhibernateLiaisonBase" xmlns="urn:nhibernate-mapping-2.2">
  <class name="CessionsLignes" table="SCESL_CESSIONSLIGNES">
    <id name="IdCessionsLignes" type="Int32">
      <generator class="identity" />
    </id>
    <property name="QuantiteCessionsLignes" column="SCESL_QTE" />
    <many-to-one name="cessions" column="SCESL_SCESID" />
  </class>
</hibernate-mapping>

any idea ?

Upvotes: 2

Views: 1817

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174477

I think you forgot the call to Column in your identity map:

Id<int>(x => x.IdCessionsLignes, map =>
{
    map.Column("SCESL_SCESID")
       .Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});

Upvotes: 3

Related Questions