摘要

NHibernate一对一关系虽然不经常碰到,但是在对于数据库结构优化的时候,经常会碰到一对一关系。比如,产品详细信息比较多的时候,可以把产品详细信息放到另一张表里面,Product主表只记录产品主要信息。这样能够显著提高产品的查询效率。

这篇文章的附件:NHibernate Demo下载。

1、建立ProductDetail表

这里将ProductId设置为主键。

Product和ProductDetail之间的关系。

ProductId既是主键又是外键。

创建ProductDetail的SQL语句

USE [NHibernateDemoDB]
GO /****** Object: Table [dbo].[ProductDetail] Script Date: 07/22/2016 23:02:25 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[ProductDetail](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ProductId] [int] NOT NULL,
[Description] [nvarchar](1000) NOT NULL,
CONSTRAINT [PK_ProductDetail] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO ALTER TABLE [dbo].[ProductDetail] WITH CHECK ADD CONSTRAINT [FK_ProductDetail_Product] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO ALTER TABLE [dbo].[ProductDetail] CHECK CONSTRAINT [FK_ProductDetail_Product]
GO

2、创建ProductDetail类,修改Product类

ProductDetail类

 namespace Demo.XML.Entities.Domain
{
public class ProductDetail
{
public virtual int Id { get; set; }
public virtual string Description { get; set; }
public virtual Product Product { get; set; }
}
}

修改Product类

 using System.Collections.Generic;

 namespace Demo.XML.Entities.Domain
{
public class Product
{
public Product()
{
Orders = new List<Order>();
} public virtual int Id { get; set; } public virtual string ProductCode { get; set; } public virtual string ProductName { get; set; } public virtual string Description { get; set; } public virtual IList<Order> Orders { get; set; } public virtual ProductDetail ProductDetail { get; set; }

public virtual void SetDetailInfo(ProductDetail detail)
{
this.ProductDetail = detail;
detail.Product = this;
}
}
}

Product和ProductDetail双向关联。

 3、添加ProductDetail.hbm.xml文件,修改Product.hbm.xml文件

ProductDetail.hbm.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Demo.XML.Entities" namespace="Demo.XML.Entities.Domain">
<class name="ProductDetail" table="ProductDetail">
<id name="Id">
<generator class="native" />
</id>
<property name="Description" not-null="true"/>
<many-to-one name="Product" unique="true" column="ProductId"/>
</class>
</hibernate-mapping>

从表ProductDetail到主表Product这里设置成many-to-one关系,unique="true"表示这个这个Product属性是唯一键,column指出对应的数据库列名。

Product.hbm.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Demo.XML.Entities" namespace="Demo.XML.Entities.Domain">
<class name="Product" table="Product">
<id name="Id">
<generator class="native" />
</id>
<property name="ProductCode" not-null="true"/>
<property name="ProductName" not-null="true"/>
<property name="Description"/>
<bag name="Orders" table="ProductOrder" cascade="all">
<key column="ProductId"/>
<many-to-many class="Order" column="OrderId"/>
</bag>
<one-to-one name="ProductDetail" cascade="all" />
</class>
</hibernate-mapping>

一对一关联使用one-to-one进行映射。这里配置成从主表Product到从表ProductDetail的级联关系为all,从表到主表没有设置级联关系。

4、关联关系的添加修改和删除

修改Main函数

 using Demo.Service;
using Demo.Service.Interface;
using Demo.XML.Entities.Domain;
using System; namespace Demo.ConsoleApp
{
class Program
{
static readonly IProductService productService = new ProductService(); static void Main(string[] args)
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); ProductDetail detail = new ProductDetail { Description = "This is a very good product" };
Product product = new Product { ProductCode = "", ProductName = "orange" };
product.SetDetailInfo(detail); int productId = productService.Save(product); product.ProductDetail.Description = "This is an excellent product";
productService.Update(product); productService.Delete(productId); Console.WriteLine("Completed");
Console.ReadLine();
}
}
}

执行该程序,得到监控结果

从监控结果看到NHibernate正确维护了表之间的一对一关系。

NHibernate系列文章二十:NHibernate关系之一对一(附程序下载)的更多相关文章

  1. NHibernate系列文章二十二:NHibernate查询之HQL查询(附程序下载)

    摘要 NHibernate提供了多种查询方式,最早的HQL语言查询.Criteria查询和SQL Query,到NHibernate 3.0的Linq NHibernate,NHIbernate 4. ...

  2. NHibernate系列文章二十八:NHibernate Mapping之Auto Mapping(附程序下载)

    摘要 上一篇文章介绍了Fluent NHibernate基础知识.但是,Fluent NHibernate提供了一种更方便的Mapping方法称为Auto Mapping.只需在代码中定义一些Conv ...

  3. NHibernate系列文章二十五:NHibernate查询之Query Over查询(附程序下载)

    摘要 这一篇文章介绍在NHibernate 3.2里引入的Query Over查询,Query Over查询跟Criteria查询类似.首先创建IQueryOver对象,然后通过调用该对象的API函数 ...

  4. NHibernate系列文章二十四:NHibernate查询之Linq查询(附程序下载)

    摘要 NHibernate从3.0开始支持Linq查询.写Linq to NHibernate查询就跟写.net linq代码一样,非常灵活,可以很容易实现复杂的查询.这篇文章使用Linq to NH ...

  5. NHibernate系列文章二十六:NHibernate查询之SQL Query查询(附程序下载)

    摘要 NHibernate在很早的版本就提供了SQL Query(原生SQL查询),对于很复杂的查询,如果使用其他的查询方式实现比较困难的时候,一般使用SQL Query.使用SQL Query是基于 ...

  6. NHibernate系列文章二:创建NHibernate工程

    摘要 这篇文章介绍了如何创建一个简单的使用NHibernate的控制台应用程序,包括使用NuGet.简单的配置.单表映射.对NHibernate配置文件添加智能提示.使用ISessionFactory ...

  7. NHibernate系列文章一:NHibernate介绍

    摘要 NHibernate是一个成熟的开源的面向对象的.net映射框架.大量的实际项目中正在使用该框架.他是建立在ADO.Net基础之上.目前的版本是NHibernate 4.0.4.本系列文章都是基 ...

  8. NHibernate系列文章十七:NHibernate Session管理(附程序下载)

    摘要 NHibernate的Session的管理涉及到NHibernate的两个最重要的对象ISessionFactory和ISession.ISessionFactory的生成非常消耗资源,通常都在 ...

  9. NHibernate系列文章八:NHibernate对象一级缓存

    摘要 Nhibernatea缓存非常强大,按照缓存存储在Session对象还是SessionFactory对象分为一级缓存和二级缓存. 一级缓存存在于Session对象里,也叫Session缓存,由S ...

随机推荐

  1. ubuntukylin14安装ns-allinone-2.35教程(虚拟机ubuntu同理)

    准备材料: 1.ubuntukylin14,百度进官网自行下载: 2.ns-allinone-2.35.tar.gz,百度进官网自行下载: 3.虚拟机:vmwareworkstation(可选). 4 ...

  2. Arcgis Server 10.2默认服务端口号修改方法

    本人安装Arcgis Server 10.2之后发布了一个地图服务,该服务默认使用的端口号是6080,本人使用的是教育网,使用教育网均能正常使用该服务,但是使用电信或者移动网络均不能正常访问该网站. ...

  3. git之.gitignore文件用途

    gitignore文件用于忽略无需追踪的文件. 配置文件: $HOME/.config/git/ignore, $GIT_DIR/info/exclude, .gitignore 举例说明: $ gi ...

  4. ping脚本

    #!/bin/bash for x in{100..200} ####区间为192.168.100.100-192.168.100.200 do x=$(($x-100)) if fping -c 1 ...

  5. JAXB最佳实践

    JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ...

  6. centos 7 mini装maridb 10.1 binary版本

    注:centos的版本为:CentOS-7-x86_64-Minimal-1503-01 http://isoredirect.centos.org/centos/7/isos/x86_64/Cent ...

  7. c语言中static的用法,包括全局变量和局部变量用static修饰

    一.c程序存储空间布局 C程序一直由下列部分组成: 1)正文段--CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令: 2)初始化数据段(数据段)--在程序中所有 ...

  8. Appium的安装

    APPium的官网地址为:http://appium.io,在官网可以看到安装步骤如下:

  9. BestCoder Round #90 A.Kblack loves flag(随机数生成种子)

    A.Kblack loves flag [题目链接]A.Kblack loves flag [题目类型]水题 &题意: kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜. 某天,k ...

  10. [MySQL] SqlServer 迁移到 MySQL 方法介绍

    一.原则: 只迁移表结构和数据,存储过程.函数.触发器尽量自己改写,并充分测试. 迁移前,先设置好数据库的一些参数,比如默认存储引擎,默认编码等,方便后续导入. 二.方法: 1.使用MySQL Wor ...