今天做了第一个Nhibernate项目,摸着石头过河,学到了一些东西,在这里将自己总结体会到的一些映射关系写出来,与大家分享,由于是初学者,如果有不对的地方希望大家能够指出来。

首先要说明要建立的几张表:(CouponType表的CouponTypeID与Merchant表中的MerchantID是多对一的关系,CouponType表中的CouponTypeID与Product表中的ProductID是多对多的关系)

CouponType表:

字段属性

字段名称

类型

非空

备注

优惠券发放编号

CouponTypeID

Int

Not null

主键、唯一性约束。自增长

优惠券类型名

Name

Varchar(100)

Not null

......

......

......

 

.......

面向商户

MerchantID

Int

Not null

外键

Merchant表:

字段属性

字段名称

类型

非空

备注

商户编号

MerchantID

Int

Not null

0为全部商户

商户名称

Name

Varchar

Not null

商户描述

Description

Varchar

Not null

Product表:

  

字段属性

字段名称

类型

非空

备注

商品编号

ProductID

Int

Not null

主键

商品名称

Name

Varchar

Not null

所属类

ProductCategoryID

Int

Not null

外键

1、多对一的映射:

首先要定义CouponType表的实体类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Giti.Domain
{
using Giti.Core;
using Iesi.Collections;
using Iesi.Collections.Generic; public class CouponType : EntityBase
{
public virtual string CouponTypeName { get; set; } .......... public virtual Merchant Merchant { get; set; }
19 }
}

然后是CouponType.hbm.xml

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Giti.Domain"
namespace="Giti.Domain"> <class name="CouponType" table="T_CouponType">
<id name="Id">
<generator class="native" />
</id>
<version name="Version" /> <property name="CouponTypeName" />
..........
<many-to-one name="Merchant" cascade="save-update"/>
.......... <property name="SortIndex" /> //一下几行代码没有特殊规定,只是为了方便维护数据库表而添加。
<property name="Active" />
<property name="CreateBy" update="false" />
<property name="UpdateBy" />
<property name="CreateDate" update="false" />
<property name="LastUpdate" />
</class>
</hibernate-mapping>

完成多对一的代码后,要马上去补充一对多的部分与之对应,详见2、一对多的映射

2、一对多的映射:

完成多对一的代码后要马上补充一对多的部分与之对应,

首先是Merchant实体类代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Giti.Domain
{
using Giti.Core;
using Iesi.Collections;
using Iesi.Collections.Generic; public class Merchant : EntityBase
{
public virtual string Name { get; set; } public virtual string Description { get; set; } private ISet<CouponType> _couponTypes = new SortedSet<CouponType>(); public virtual ISet<CouponType> CouponTypes
{
get { return _couponTypes; }
set { _couponTypes = value; }
} } }

然后看一下Merchant.hbm.xml的代码:

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Giti.Domain"
namespace="Giti.Domain"> <class name="Merchant" table="T_Merchant">
<id name="Id">
<generator class="native" />
</id>
<version name="Version" /> <property name="Name" />
<property name="Description" /> <set name="CouponTypes" inverse="true" cascade="none" lazy="true">
<key column="Merchant" />
<one-to-many class="Giti.Domain.CouponType, Giti.Domain"/>
</set> <property name="SortIndex" />
<property name="Active" />
<property name="CreateBy" update="false" />
<property name="UpdateBy" />
<property name="CreateDate" update="false" />
<property name="LastUpdate" />
</class>
</hibernate-mapping>

到此一组“多对一”和“一对多”的关系已经建立起来

3、多对多的映射(需要建表):

下面我们看一下多对多的关系,多对多的关系如果通过外键关联,则会造成数据库设计的不规范,所以当有多对多的关系出现时我们采用建一张关系表的方式来解决。

CouponType中CouponTypeID与Product中的ProductID是多对多的关系,所以要在上面CouponTyoe实体类的基础上添加一些关系(黄色底部分为添加代码),代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Giti.Domain
{
using Giti.Core;
using Iesi.Collections;
using Iesi.Collections.Generic; public class CouponType : EntityBase
{
public virtual string CouponTypeName { get; set; } ......... public virtual Merchant Merchant { get; set; } private ISet<Product> _products = new SortedSet<Product>(); public virtual ISet<Product> Products
{
get { return _products; }
set { _products = value; }
}
}
}

CouponType.hbm.xml

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Giti.Domain"
namespace="Giti.Domain"> <class name="CouponType" table="T_CouponType">
<id name="Id">
<generator class="native" />
</id>
<version name="Version" /> <property name="CouponTypeName" />
.......
<many-to-one name="Merchant" cascade="save-update"/> <set name="Products" inverse="false" cascade="save-update" table="T_Coupon_Product">
<key column="CouponTypeId" />
<many-to-many column="ProductId" class="Giti.Domain.Product, Giti.Domain" />
</set>
     <property name="SortIndex" />
<property name="Active" />
<property name="CreateBy" update="false" />
<property name="UpdateBy" />
<property name="CreateDate" update="false" />
<property name="LastUpdate" />
</class>
</hibernate-mapping>

注意与之前代码不同,这里要建表!

然后就是Product的实体类和Product.hbm.xml

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Giti.Domain
{
using Giti.Core;
using Iesi.Collections;
using Iesi.Collections.Generic; public class Product:EntityBase
{
public virtual string Name { get; set; } private ISet<CouponType> _couponTypes = new SortedSet<CouponType>(); public virtual ISet<CouponType> CouponTypes
{
get { return _couponTypes; }
set { _couponTypes = value; }
}
}
}
 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Giti.Domain"
namespace="Giti.Domain"> <class name="Product" table="T_Product">
<id name="Id">
<generator class="native" />
</id>
<version name="Version" /> <property name="Name" /> <set name="CouponTypes" inverse="false" cascade="save-update" table="T_Coupon_Product">
<key column="ProductId" />
<many-to-many column="CouponTypeId" class="Giti.Domain.CouponType, Giti.Domain" />
</set>
<property name="SortIndex" />
<property name="Active" />
<property name="CreateBy" update="false" />
<property name="UpdateBy" />
<property name="CreateDate" update="false" />
<property name="LastUpdate" />
</class>
</hibernate-mapping>

到此三个映射关系已基本完成,在配置过程中我最大的感觉是映射关系配置要两两一起配置,这样思路会更清晰,配置过程也不容易出错。

Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。的更多相关文章

  1. Hibernate关联映射(一对多/多对多)

    版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/843401053.  Hibernate关联映射上接Hibernate持久化类:h ...

  2. Hibernate映射关系之_多对多

    多对多关系由于效率的原因,实际中会拆成相互的一对多的关系,不再累述

  3. MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制

    映射(多.一)对一的关联关系 1)若只想得到关联对象的id属性,不用关联数据表 2)若希望得到关联对象的其他属性,要关联其数据表 举例: 员工与部门的映射关系为:多对一 1.创建表 员工表 确定其外键 ...

  4. 2018.11.4 Hibernate中一对、多对多的关系

    简单总结一下 多表关系 一对多/多对一 O 对象 一的一方使用集合. 多的一方直接引用一的一方. R 关系型数据库 多的一方使用外键引用一的一方主键. M 映射文件 一: 多: 操作: 操作管理级别属 ...

  5. hibernate--关联映射(一对多)

    在对象模型中,一对多的关联关系,使用集合来表示. 实例场景:班级对学生:Classes(班级)和Student(学生)之间是一对多的关系. 对象模型: 多对一.一对多的区别: 多对一关联映射:在多的一 ...

  6. hibernate_08_关联映射_一对多

    hibernate的映射关系 一对多.多对一.一对一.多对多. 常用的是一对多和多对一. 在数据库中可以通过添加主外键的关联,表现一对多的关系:在hibernate中通过在一方持有多方的集合实现,即在 ...

  7. Nhibernate 一对一,一对多,多对多 成功映射

    前语: 在Nhibernate xml 的文件配置上,一对一和多对多的配置比较简单,容易出错的反而是一对多(多对一)上. 1.一对一关联关系的映射: <one-to-one name=" ...

  8. Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射

    1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; privat ...

  9. JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析

    JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...

随机推荐

  1. flex与js相互调用

    1.flex调用js方法 调用方法例如:ExternalInterface.call("UploadComplete",oldName,uidName,_dir+"/&q ...

  2. JQ 操作样式,背景切换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. WindowsForm 打印

    打印: 打印对话框:printdialog 页面设置:pagesetupdialog 这两个对话框都需要通过设置printdocument来指定打印对象 printdocument:打印对象,必须要有 ...

  4. C++程序设计实践指导1.5求两个整数集合并集改写要求实现

    改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; ...

  5. 也谈谈关于WEB的感想

    距离上次在博客园发表博文已经是数年以前了,想想自己也确实有够懒惰的,实为不该. 引起我想发这篇博文的原因是 @Charlie.Zheng 所发表的 <Web系统开发构架再思考-前后端的完全分离& ...

  6. 容联手机接口封装到ThinkPHP3.2.菜鸟图文教学

    今天来说下短信发送技术. 使用的是 容联http://www.yuntongxun.com/ 用法很简单, 具体要知道的参数有 ACCOUNT SID   应用ID AUTH TOKEN 应用toke ...

  7. tornado.web.Application类配置及使用

    Application configuration classtornado.web.Application(handlers=None, default_host='', transforms=No ...

  8. .Net MVC4 加.html后缀报404问题

    一.配置方法: routes.MapRoute( name: "User_Default1", url: "user/{action}.html", defau ...

  9. 轻松背后的N+疲惫——系统日志

    相信很多coder都有这样的癖好:“自恋”!!对自己编写的code总是那么的自信,自豪,Always believe it to be so perfect!! 不喜欢做单元测试(总觉得它就那样了能出 ...

  10. python操作redis--string

    #!/usr/bin/python #!coding:utf-8 """ 完成用redis模块操作string类型的数据 """ impor ...