NHibernate使用入门(十)
来源:互联网 发布:书里的句子 知乎 编辑:程序博客网 时间:2024/06/10 09:58
多对一双向映射
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(); } }
运行结果如图:
0 0
- NHibernate使用入门(十)
- NHibernate使用入门
- NHibernate使用入门(一)
- NHibernate使用入门(二)
- NHibernate使用入门(三)
- NHibernate使用入门(四)
- NHibernate使用入门(五)
- NHibernate使用入门(六)
- NHibernate使用入门(七)
- NHibernate使用入门(八)
- NHibernate使用入门(九)
- NHibernate使用入门(十一)
- NHibernate使用入门(十二)
- NHibernate使用入门(十三)
- NHibernate使用入门(十四)
- 十、NHIbernate之使用视图
- nhibernate入门系列: 使用Criteria载入对象
- nhibernate入门系列: 使用Criteria载入对象
- Android之SurfaceView使用总结
- Android_Sqlbrite入门使用
- PMP考试答题技巧24计
- caffe zoo .caffemodels模型下载
- java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得
- NHibernate使用入门(十)
- node怎么禁用favicon.ico请求
- 百度地图SDK使用及配置到自己的工程项目中
- MIME: s3css文件按照css加载但是返回了个application/octet-stream
- BUNOJ 1011
- AFHTTPSessionManager随机生成boundary
- springMVC 自定义拦截器
- IONIC下屏幕向上滑动时渐变显示隐藏的标题栏
- C++类中的隐式类型转换