Iesi Collection在Nhibernate工作

时间:2013-02-14 06:45:55

标签: asp.net-mvc nhibernate

您好我正在使用NHibernate创建应用程序。当我使用Collection映射两个表时,我面临的是它没有在父表中插入值,而只是在子表中插入数据。这是相同的代码。

CustomerInfo.cs

public class CustomerInfo
    {

        int _CustomerId;
        public virtual int CustomerId
        {
            get { return _CustomerId; }
            set { _CustomerId = value; }
        }

        string _Name;
        [Required]
        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        [Required]
        public virtual string Address { get; set; }


        string _City;
        public virtual string City
        {
            get { return _City; }
            set { _City = value; }
        }

        string _Phone;
        [Required]
        public virtual string Phone
        {
            get { return _Phone; }
            set { _Phone = value; }
        }

        string _EmailId;
        [Required]
        public virtual string EmailID
        {
            get { return _EmailId; }
            set { _EmailId = value; }
        }


        string _UserName;
        [Required]
        public virtual string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }


        string _Password;
        [Required]
        public virtual string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        int _IsActive;

        public virtual int IsActive
        {
            get { return _IsActive; }
            set { _IsActive = value; }
        }


        public virtual ISet<BankAccount> BankAccounts { get; set; }

        public CustomerInfo()
        {
            BankAccounts = new HashedSet<BankAccount>();
        }

    }

BankAccount.cs

 public class BankAccount: CustomerInfo
    {

        int _AccountId;
        [Required]
        public virtual int AccountId
        {
            get { return _AccountId; }
            set { _AccountId = value; }
        }


        string _AccountNumber;
        [Required]
        public virtual string AccountNumber
        {
            get { return _AccountNumber; }
            set { _AccountNumber = value; }
        }


        string _AccountType;
        [Required]
        public virtual string AccountType
        {
            get { return _AccountType; }
            set { _AccountType = value; }
        }

        int _IsAccountActive;

        public virtual int IsAccountActive
        {
            get { return _IsAccountActive; }
            set { _IsAccountActive = value; }
        }

      public virtual CustomerInfo CustomerAccount { get; set; }

    }

这是映射文件..

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
 xmlns="urn:nhibernate-mapping-2.2"
 assembly="NhibernateApplication"
 namespace="NhibernateApplication"
 auto-import="true">

  <!--discriminator-value="C"-->
  <class name="NhibernateApplication.Models.CustomerInfo,NhibernateApplication" >

    <id name="CustomerId" access="property" column="CustomerID" type="Int32">
      <generator class="native"></generator>
    </id>

    <!--<discriminator column="DISCRIMINATOR" type="string" />-->

    <property name="Name" access="property"
     column="Name" type="String"></property>

    <property name="Address" access="property"
     column="Address" type="String"></property>

    <property name="City" access="property"
     column="City" type="String"></property>

    <property name="Phone" access="property"
     column="Phone" type="String"></property>

    <property name="EmailID" access="property"
     column="EmailID" type="String"></property>

    <property name="UserName" access="property"
     column="UserName" type="String"></property>

    <property name="Password" access="property"
     column="Password" type="String"></property>

    <property name="IsActive" access="property"
     column="IsActive" type="Int32"></property>


    <!--<subclass name="NhibernateApplication.Models.BankAccount,NhibernateApplication" 
              extends="NhibernateApplication.Models.CustomerInfo,NhibernateApplication" discriminator-value="A">
      <property name="AccountNumber" access="property"
      column="AccountNumber" type="String"></property>
      <property name="AccountType" access="property"
       column="AccountType" type="String"></property>
    </subclass>-->
      <set inverse="true" name="BankAccounts"
           mutable="true" cascade="save-update">
        <key>
          <column name="CustomerId" not-null="true" />
        </key>
        <one-to-many  class="NhibernateApplication.Models.BankAccount,NhibernateApplication" />
      </set>
   </class>
</hibernate-mapping>

Bank Mapping.hbm

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
 xmlns="urn:nhibernate-mapping-2.2"
 assembly="NhibernateApplication"
 namespace="NhibernateApplication"
 auto-import="true">

  <class name="NhibernateApplication.Models.BankAccount,NhibernateApplication" discriminator-value="C">

    <id name="AccountId" access="property" column="AccountId" type="Int32">
      <generator class="native"></generator>
    </id>

    <discriminator column="DISCRIMINATOR" type="string" />

    <property name="AccountType" access="property"
     column="AccountType" type="String"></property>

    <property name="AccountNumber" access="property"
       column="AccountNumber" type="String"></property>

    <many-to-one class="NhibernateApplication.Models.CustomerInfo"  name="CustomerAccount" column ="CustomerId" />
  </class>
</hibernate-mapping>

这就是我调用createQuery的方式

public int CreateBankAccount(BankAccount bct)
        {
            int cusId = 0;

            using (ISession session = OpenSession())
            {
                //Perform transaction
                using (ITransaction tran = session.BeginTransaction())
                {
                    session.Persist(bct.CustomerAccount);
                    tran.Commit();
                }
            }

            return cusId;
        }

0 个答案:

没有答案