2

我是 Nhibernate 的新手。希望我能在这里找到答案。这是我的课:

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

这是我的映射:

<?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>

当我尝试在会话工厂中添加存储库程序集时,这是代码

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

        log4net.Config.XmlConfigurator.Configure();

        config.Configure();

        _sessionFactory = config.BuildSessionFactory();

给我错误:命名空间“urn:nhibernate-mapping-2.2”中的元素“类”在命名空间“urn:nhibernate-mapping-2.2”中有无效的子元素“id”。

谢谢!

4

1 回答 1

1

您有一个composite-id元素后跟一个id元素。你不能两者兼得。

composite-id当表中的主键跨越多个列时使用。

除此之外,您只能拥有一个id元素。它定义了如何唯一标识您的实体。对于属性,您应该使用property元素。

查看 NHibernate 文档可能会有所帮助: http ://www.nhforge.org/doc/nh/en/#mapping

于 2012-06-22T17:05:16.353 回答