NHibernate使用入门(十)

多对一双向映射

Address.hbm.xml映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"
                   assembly="NtestRelationship"
                   namespace="NtestRelationship.Entities"
                   >
  <class name="Address">
    <id type="int" name="Id">
      <column name="AddressId" />
      <generator class="identity"></generator>
    </id>
    <property name="Country"/>
    <property name="Street"/>
    <property name="StreetNumber"/>
    <property name="PostalCode"/>
    <property name="City"/>

    <one-to-one name="Customer" />

  </class>
</hibernate-mapping>

Customer.hbm.xml映射文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"
                   assembly="NtestRelationship"
                   namespace="NtestRelationship.Entities"
                   >
  <class name="Customer">
    <id type="int" name="Id">
      <column name="Id" />
      <generator class="identity"></generator>
    </id>
    <property name="FirstName"/>
    <property name="LastName"/>

    <many-to-one name="Address" not-null="false" column="addressId" unique="true" ></many-to-one>
  </class>
</hibernate-mapping>

ManyToOneRelationBidirectional 类

  public class ManyToOneRelationBidirectional 
    {
        private readonly ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
        protected object CustomerId=1;
        protected object AddressId=1;

        public void Customer_Address_OneWayRelation()
        {
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                Customer customer = session.Get<Customer>(CustomerId);
                Address address = session.Get<Address>(AddressId);

                customer.Address = address;
                session.Save(customer);
                transaction.Commit();
            }

            using (var session = sessionFactory.OpenSession())
            {
                Customer customerSecond = session.Get<Customer>(CustomerId);

                Console.WriteLine("customerSecond.Address NotBeNull IS:" + (customerSecond.Address != null)); 
            }
        }
        public void Customer_Address_TwoWayRelation()
        {
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                Customer customer = session.Get<Customer>(CustomerId);
                Address address = session.Get<Address>(AddressId);

                customer.Address = address;
                session.Save(customer);
                transaction.Commit();
            }

            using (var session = sessionFactory.OpenSession())
            {
                Customer customerSecond = session.Get<Customer>(CustomerId);
                Address addressSecond = session.Get<Address>(AddressId);

                Console.WriteLine("addressSecond.Customer NotBeNull IS:" + (addressSecond.Customer != null));
                Console.WriteLine("customerSecond.Address NotBeNull IS:" + (customerSecond.Address != null)); 

            }

        }

        public void Customer_Address_TwoWayRelationAssignCustomerToAddress()
        {
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                Customer customer = session.Get<Customer>(CustomerId);
                Address address = session.Get<Address>(AddressId);

                customer.Address = address;
                address.Customer = customer;

                session.Save(customer);

                transaction.Commit();
            }

            using (var session = sessionFactory.OpenSession())
            {
                Customer customerSecond = session.Get<Customer>(CustomerId);
                Address addressSecond = session.Get<Address>(AddressId);

                Console.WriteLine("addressSecond.Customer NotBeNull IS:" + (addressSecond.Customer != null));
                Console.WriteLine("customerSecond.Address NotBeNull IS:" + (customerSecond.Address != null)); 

            }
        }
    }

主程序

 class Program
    {
        static void Main(string[] args)
        {
           ManyToOneRelationBidirectional mtmr=new ManyToOneRelationBidirectional();
            mtmr.Customer_Address_OneWayRelation();
            mtmr.Customer_Address_TwoWayRelation();
            mtmr.Customer_Address_TwoWayRelationAssignCustomerToAddress();
            Console.ReadKey();
        }
    }

运行结果如图:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值