今天做了第一个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. 关于JS变量和作用域

    ECMAScript 变量:1.基本类型值(简单数据段) 2.引用类型值(可能由过个值构成的对象) → 保存在内存中的对象 动态属性: 只能给引用型值动态添加新属性,以便将来使用. 复制变量值 : 基 ...

  2. struts1:(Struts重构)构建一个简单的基于MVC模式的JavaWeb

    在构建一个简单的基于MVC模式的JavaWeb 中,我们使用了JSP+Servlet+JavaBean构建了一个基于MVC模式的简单登录系统,但在其小结中已经指出,这种模式下的Controller 和 ...

  3. css基础之 图片瀑布流布局:用CSS+DIV等宽格子堆砌瀑布流效果 (一)

    <!doctype html> <html> <head> <meta charset="UTF-8"/> <title> ...

  4. IE的documentMode属性

    参看下面链接:<IE的documentModeshuxing>

  5. AngularJS 之Services讲解

    Angular带来了很多类型的services.每个都会它自己不同的使用场景.我们将在本节来阐述. 首先我们必须记在心里的是所有的services都是singleton(单例)的,这也是我们所希望得到 ...

  6. 【应用】Markdown 在线阅读器

    前言 一款在线的 Markdown 阅读器,主要用来展示 Markdown 内容.支持 HTML 导出,同时可以方便的添加扩展功能.在这个阅读器的基础又做了一款在线 Github Pages 页面生成 ...

  7. flask 后台表单验证模块

    我不想直接用flask的wtf模块,太大,功能太多,用不了.但后台也不能不做验证吧,我比较懒,不想一行一行写代码验证,所以就写了一个验证模块,对于小项目也够用了 # encoding=utf-8 # ...

  8. Python中yield深入理解

    众所周知,python中的yield有这样的用法: def test(alist): for i in alist: yield i 这样,这个test函数就变成了一个生成器,当每次调用的时候,就会自 ...

  9. Outlook2007、2010和Foxmail的簽名設計

    由於個人習慣問題公司大部分人採用第三方郵件工具,對與郵件的通訊設置大家完全可以通過嚮導完成,但是郵件的簽名設計往往隐藏了起来,现在就由我来带大家进行个性签名设计. Outlook2007 第一步: 点 ...

  10. 解决同一PC同一浏览器session共享问题

    首先session是同一PC同一浏览器共享的.比如如下代码: public void doPost(HttpServletRequest request, HttpServletResponse re ...