2
votes

I'm newbie with Nhibernate. Hope I can find the answer here. This is my class:

public class PaymentAudit : EntityBase<int>, IAggregateRoot
{
    public PaymentAudit() { }
    public  System.Guid PaymentStateId { get; set; }
    public  System.DateTime DateStamp { get; set; }
    public  Payment Trn { get; set; }
    public  PaymentSaga PaymentSaga { get; set; }
    public  ProcessState ProcessState { get; set; }
    public  PublishState PublishState { get; set; }
    public  System.Nullable<short> ChgCount { get; set; }
    public  string UserName { get; set; }

    protected override void Validate()
    {
    }
}

This is my mapping:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Praweda.Interface.Framework.Model.Payment"
        assembly="Praweda.Interface.Framework.Model">

  <class name="PaymentAudit" table="PaymentAudit" lazy="false" >
    <composite-id>
      <key-property name="PaymentStateId" column="PaymentStateId" />
      <key-property name="DateStamp" column="DateStamp" />
      <key-property name="ProcessState" column="ProcessState" />
    </composite-id>
    <id name="PaymentState">
      <generator class="identity" />
      <column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
    </id>    
    <id name="ProcessState">
      <generator class="identity" />
      <column name="ProcessState" sql-type="varchar" not-null="true" />
    </id>
    <id name="DateStamp">
      <generator class="identity" />
      <column name="DateStamp" sql-type="datetime" not-null="true" />
    </id>   
    <many-to-one insert="false" update="false" lazy="false" name="PaymentStateId">
      <column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
    </many-to-one>
    <property name="PaymentStateId">
      <column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="ProcessState">
      <column name="ProcessState" sql-type="varchar" not-null="true" />
    </many-to-one>
    <property name="ProcessState">
      <column name="ProcessState" sql-type="varchar" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="PublishState">
      <column name="PublishState" sql-type="varchar" not-null="false" />
    </many-to-one>
    <property name="PublishState">
      <column name="PublishState" sql-type="varchar" not-null="false" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="Trn">
      <column name="Trn" sql-type="varchar" not-null="false" />
    </many-to-one>
    <property name="Trn">
      <column name="Trn" sql-type="varchar" not-null="false" />
    </property>
    <property name="ChgCount">
      <column name="ChgCount" sql-type="smallint" not-null="false" />
    </property>
    <property name="UserName">
      <column name="UserName" sql-type="nvarchar" not-null="false" />
    </property>
  </class>

</hibernate-mapping>

When I'm trying to add the repository assembly in session factory, here's the code

        Configuration config = new Configuration();
        config.AddAssembly("MyProject.Nhibernate.Repository");

        log4net.Config.XmlConfigurator.Configure();

        config.Configure();

        _sessionFactory = config.BuildSessionFactory();

give me error : The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'id' in namespace 'urn:nhibernate-mapping-2.2'.

Thanks!

1

1 Answers

1
votes

You have a composite-id element followed by an id element. You can't have both.

Use composite-id when the primary key in your table spans multiple columns.

Next to that, you can only have one single id element. It defines how your entity is uniquely identified. For properties, you should use the property element.

It might be helpfull to check out the NHibernate documentation: http://www.nhforge.org/doc/nh/en/#mapping