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 ...
随机推荐
- javascirpt的apply和call
javascirpt的apply和call用法如下: var arr1=new Array("1","2","3"); var a ...
- hbase性能调优之压缩测试
文章概述: 1.顺序写 2.顺序读 3.随机写 4.随机读 5.SCAN数据 0 性能测试工具 hbase org.apache.hadoop.hbase.PerformanceEvaluation ...
- has leaked ServiceConnection com.baidu.location.LocationClient
02-06 05:01:52.806: E/ActivityThread(1120): Activity com.project.xxxActivity $1@45fc5af8 that was or ...
- c语言函数注意点
返回值的基本概念.return的作用 return 1>void可以省略return 2>可以多次使用return 3>return后面不能有其他语句 4. 函数的弱语法 ...
- Http和Socket连接
转自http://hi.baidu.com/%D2%B9%D1%A9%B3%E6/blog/item/d6a72d2bbf467cf2e7cd406d.html 相信不少初学手机联网开发的朋友都想知道 ...
- SQL Server 内存压力解决方案
外部压力: 表现形式: 1.total server memory ↓ 2.avilable Mbyte 平衡 3.working set ↓ 如果说SQ ...
- gdal vc++ 配置说明
1在VC中,打开菜Tool-Option,在Directories页面中的Library files中和Include files中分别添加GDAL的LIB文件目录和INCLUDE文件目录2打开菜 ...
- Amzon MWS API开发之 上传数据
亚马逊上传数据,现有能操作的功能有很多:库存数量.跟踪号.价格.商品....... 我们可以设置FeedType值,根据需要,再上传对应的xml文件即可. 下面可以看看FeedType类型 这次我们拿 ...
- Genymotion如何访问本地服务器?
找到原因了,其实跟Genymotion没有关系,因为他本身是作为VirtualBox的一个虚拟OS在运行. 默认情况下,查看Genymotion的网络配置,是Host-Only模式: Microsof ...
- [置顶] Oracle GoldenGate 系列:使用 Oracle ASM API DBLOGREADER 时遇 ora-01031 错误
今天在自己新搭建的 Oracle ACFS 文件系统上测试 GoldenGate ,启动 extract 进程报如下错误: 2013-08-27 14:58:39 ERROR OGG-00446 ...