NHibernate使用入门(十)

时间:2022-02-20 08:22:29

多对一双向映射

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

运行结果如图:

NHibernate使用入门(十)