Nhibernate 映射关系,一对多 多对一与多对手在映射文件中的体现。
今天做了第一个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 映射关系,一对多 多对一与多对手在映射文件中的体现。的更多相关文章
- Hibernate关联映射(一对多/多对多)
版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/843401053. Hibernate关联映射上接Hibernate持久化类:h ...
- Hibernate映射关系之_多对多
多对多关系由于效率的原因,实际中会拆成相互的一对多的关系,不再累述
- MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制
映射(多.一)对一的关联关系 1)若只想得到关联对象的id属性,不用关联数据表 2)若希望得到关联对象的其他属性,要关联其数据表 举例: 员工与部门的映射关系为:多对一 1.创建表 员工表 确定其外键 ...
- 2018.11.4 Hibernate中一对、多对多的关系
简单总结一下 多表关系 一对多/多对一 O 对象 一的一方使用集合. 多的一方直接引用一的一方. R 关系型数据库 多的一方使用外键引用一的一方主键. M 映射文件 一: 多: 操作: 操作管理级别属 ...
- hibernate--关联映射(一对多)
在对象模型中,一对多的关联关系,使用集合来表示. 实例场景:班级对学生:Classes(班级)和Student(学生)之间是一对多的关系. 对象模型: 多对一.一对多的区别: 多对一关联映射:在多的一 ...
- hibernate_08_关联映射_一对多
hibernate的映射关系 一对多.多对一.一对一.多对多. 常用的是一对多和多对一. 在数据库中可以通过添加主外键的关联,表现一对多的关系:在hibernate中通过在一方持有多方的集合实现,即在 ...
- Nhibernate 一对一,一对多,多对多 成功映射
前语: 在Nhibernate xml 的文件配置上,一对一和多对多的配置比较简单,容易出错的反而是一对多(多对一)上. 1.一对一关联关系的映射: <one-to-one name=" ...
- Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射
1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; privat ...
- JPA实体关系映射:@ManyToMany多对多关系、@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析
JPA实体关系映射:@ManyToMany多对多关系.@OneToMany@ManyToOne一对多多对一关系和@OneToOne的深度实例解析 今天程序中遇到的错误一 org.hibernate.A ...
随机推荐
- MVC 全局异常过滤器HandleErrorAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- linux网络编程:select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)
从别人的博客中转载过来了这一篇文章,经过重新编辑排版之后展现于此,做一个知识点保存与学习. select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复 ...
- mysql数据备份和还原命令
mysql数据库备份和还原 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfil ...
- 一个纯CSS DIV天气动画图标【转扒的】
<p> </p> <style><!-- /* SUNNY */ .sunny { -webkit-animation: sunny 15s linear i ...
- Java编程思想——类型信息(RTTI)
一.概念 编译时已知的到所有的类型:就是在写代码阶段就确定是这个类型了,当运行程序的时候,类型是不可改变的 举例:List<String> str = new ArrayList(); ...
- SquirrelMQ消息队列
SquirrelMQ是一个快速的消息队列. SquirrelMQ特性: 1. SquirrelMQ使用Slab内存分配算法来降低内存碎片,使用epoll来解决高并发问题.效率比redis要高,使用简单 ...
- Joomla JEvents 组件
JEvents http://extensions.joomla.org/extensions/extension/calendars-a-events/events/jevents Getting ...
- twisted 使用
工欲善其事,必先利其器,我们先来进行 twisted 框架的安装,由于平时使用的都是 Windows 系统,那么下面我们就讲解下 Windows 下 twisted 框架的安装(1)下载 twiste ...
- Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
1,引言 最近一直在看Scrapy 爬虫框架,并尝试使用Scrapy框架写一个可以实现网页信息采集的简单的小程序.尝试过程中遇到了很多小问题,希望大家多多指教. 本文主要介绍如何使用Scrapy结合P ...
- To and Fro(字符串水题)
To and Fro 点我 Problem Description Mo and Larry have devised a way of encrypting messages. They first ...