今天做了第一个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. JQ each

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

  2. JQ 复制节点

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

  3. HDU 5730 - Shell Necklace

    题意: 给出连续的1-n个珠子的涂色方法 a[i](1<=i<=n), 问长度为n的珠链共有多少种涂色方案 分析: 可以得到DP方程: DP[n] = ∑(i=1,n) (DP[n-i]* ...

  4. 单光纤udp通信

    环境:      两块板子,拥有独立系统(Linux),通过单光纤连接(数据只能单向发送,无反馈).两块板子采用udp协议通信. 问题: 发送板子发送数据后,接收板子上的进程收不到数据. 确认两块光纤 ...

  5. CDZSC_2015寒假新人(1)——基础 c

    Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wareho ...

  6. Shell学习之Shift的用法

        位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1 ...

  7. iOS Dev (21) 用 AVPlayer 播放一个本地音频文件

    iOS Dev (21) 用 AVPlayer 播放一个本地音频文件 作者:CSDN 大锐哥 博客:http://blog.csdn.net/prevention 前言 这篇文章与上一篇极其相似,要注 ...

  8. mysql的四种隔离级别

    一.READ UNCOMMITTED(未提交读) 在READ UNCOMMITTED级别,事务中的修改,即使未提交,对其他事务也都是可见的.事务可以读取未提交的数据,这也被称为脏读( Dirty RE ...

  9. View的工作原理(二)——layout

    1.当View的measure被确定后,会调用ViewGroup的layout方法,之后使用onLayout方法(同样也是系统未自动重写,要我们自己完成)遍历子View(根Measure的形式是一样得 ...

  10. 循环-10. 求序列前N项和*

    /* * Main.c * C10-循环-10. 求序列前N项和 * Created on: 2014年7月30日 * Author: Boomkeeper *******部分通过******* */ ...