NHibernate使用入门(九)

多对一单向映射:多对一的映射关系是在多的一端进行维护的,一对多关系映射则由一端来维护这种关系。

Customer.cs类和Customer.hbm.xml实体映射文件

 [Serializable]
    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Address Address { get; set; }

        public byte[] DbVersion { get; set; }
        public int Version { get; set; }
        public DateTime Timestamp { get; set; }

        public IList<Order> Orders { get; set; }

        public Customer()
        {
            Orders = new List<Order>();
        }
    }

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

Address.cs类和Address.hbm.xml实体映射文件

  [Serializable]
    public class Address
    {
        public int Id { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string StreetNumber { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public Customer Customer { get; set; }
    }

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


  </class>
</hibernate-mapping>

ManyToOneRelation.cs类

 public class ManyToOneRelation  
    {
        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("Customer NotBeNull IS:" + (customerSecond != null));
            }
        }
    }

主程序

 class Program
    {
        static void Main(string[] args)
        {
            ManyToOneRelation mr=new ManyToOneRelation();
            mr.Customer_Address_OneWayRelation();

            Console.ReadKey();
        }
    }

运行结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值