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="foreign">
        <param name="property">Customer</param>
      </generator>
    </id>
    <property name="Country"/>
    <property name="Street"/>
    <property name="StreetNumber"/>
    <property name="PostalCode"/>
    <property name="City"/>
    <one-to-one name="Customer" constrained="true" />
  </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"/>

    <one-to-one name="Address" />
  </class>
</hibernate-mapping>

OneToOneRelationBidirectional.cs类

 public class OneToOneRelationBidirectional
    {
        private readonly ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
        public void AddCustomerToAddress()
        {
            object addressId;
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                Customer customer = new Customer() { FirstName = "wu", LastName = "Lex" };
                Address address = new Address()
                {
                    City = "Nantong",
                    Country = "China",
                    PostalCode = "200110",
                    Street = "king",
                    StreetNumber = "12345"
                };

                address.Customer = customer;

                addressId = session.Save(address);

                transaction.Commit();
            }

            using (var session = sessionFactory.OpenSession())
            {
                Address addressSecond = session.Get<Address>(addressId);

                Console.WriteLine("addressSecond.Customer NotBeNull IS :" + (addressSecond.Customer != null));
                Customer customerSecond = session.Get<Customer>(addressSecond.Customer.Id);
                Console.WriteLine("customerSecond.Address NotBeNull IS :" + (customerSecond.Address != null));
            }
        }

    }

主程序:

class Program
    {
        static void Main(string[] args)
        {
            OneToOneRelationBidirectional otor = new OneToOneRelationBidirectional();
            otor.AddCustomerToAddress();
            Console.ReadKey();
        }
    }

运行结果:

这里写图片描述


这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值