NHibernate学习笔记(5)—正向与反向生成
2009-10-07 20:06 小郝(Kaibo Hao) 阅读(504) 评论(1) 编辑 收藏 举报
NHibernate之旅(18):初探代码生成工具使用(new!)、NHibernate之旅(19):初探SchemaExport工具使用(new!)、NHibernate之旅(20):再探SchemaExport工具使用(new!)这三篇文章后。自己也试了试。发现代码生成没有什么变化。对于NHibernate2.1.0这个版本在NHibernate之旅(19):初探SchemaExport工具使用(new!)中说道的,SchemaExport类的Execute方法已经没有四个参数的重载了。原来那个控制输出SQL格式的bool类型参数的参数format取消了。只有前三个参数保留的那个方法。相同现象出现在SchemaExport.Execute的六个参重载的方法中,也同样省略掉那个format参数。在做测试时,要注意下。
在做SchemaExport.Execute测试的时候发现在Order.hbm.xml中明明已经设置了的“级联删除”属性,但是却没有在生成的数据库中有任何作用。我的设置如下:
继续,看了在做SchemaExport.Execute测试的时候发现在Order.hbm.xml中明明已经设置了的“级联删除”属性,但是却没有在生成的数据库中有任何作用。我的设置如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name="DomainModel.Entities.Order,DomainModel" table="[Order]" >
<id name="OrderId" column="OrderId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="OrderDate" column="OrderDate" type="DateTime"
not-null="true" />
<!--多对一关系:Orders属于一个Customer-->
<many-to-one cascade="all" name="Customer" column="Customer" not-null="true"
class="DomainModel.Entities.Customer,DomainModel"
foreign-key="FK_CustomerOrders" />
<!--多对多关系:Order有多个Products-->
<bag name="Products" cascade="all" generic="true" table="OrderProduct">
<key column="[Order]" foreign-key="FK_OrderProducts"/>
<many-to-many column="Product"
class ="DomainModel.Entities.Product,DomainModel"
foreign-key="FK_ProductOrders"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DomainModel" namespace="DomainModel">
<class name="DomainModel.Entities.Order,DomainModel" table="[Order]" >
<id name="OrderId" column="OrderId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="OrderDate" column="OrderDate" type="DateTime"
not-null="true" />
<!--多对一关系:Orders属于一个Customer-->
<many-to-one cascade="all" name="Customer" column="Customer" not-null="true"
class="DomainModel.Entities.Customer,DomainModel"
foreign-key="FK_CustomerOrders" />
<!--多对多关系:Order有多个Products-->
<bag name="Products" cascade="all" generic="true" table="OrderProduct">
<key column="[Order]" foreign-key="FK_OrderProducts"/>
<many-to-many column="Product"
class ="DomainModel.Entities.Product,DomainModel"
foreign-key="FK_ProductOrders"/>
</bag>
</class>
</hibernate-mapping>
请看黄色背景色标注的地方,明明已经设置了 cascade="all"
但是却没有起作用。另外在2.1.0版本中解决李永京所说的SchemaUpdate类不能更新字段长度的问题,但是非空还是没有做到更新,只能期待下一个版本了。
下面是SchemaUpdate生成的SQL
------ Test started: Assembly: DALTest.dll ------
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_CustomerOrders]') AND parent_object_id = OBJECT_ID('[Order]'))
alter table [Order] drop constraint FK_CustomerOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_ProductOrders]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_ProductOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_OrderProducts]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_OrderProducts
if exists (select * from dbo.sysobjects where id = object_id(N'[Order]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Order]
if exists (select * from dbo.sysobjects where id = object_id(N'OrderProduct') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table OrderProduct
if exists (select * from dbo.sysobjects where id = object_id(N'Customer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Customer
if exists (select * from dbo.sysobjects where id = object_id(N'viewCustomer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table viewCustomer
if exists (select * from dbo.sysobjects where id = object_id(N'Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Product
create table [Order] (
OrderId INT IDENTITY NOT NULL,
OrderDate DATETIME not null,
Customer INT not null,
primary key (OrderId)
)
create table OrderProduct (
[Order] INT not null,
Product INT not null
)
create table Customer (
CustomerId INT IDENTITY NOT NULL,
Version INT not null,
Firstname NVARCHAR(50) null,
Lastname NVARCHAR(50) null,
primary key (CustomerId),
unique (Firstname, Lastname)
)
create table viewCustomer (
CustomerId INT IDENTITY NOT NULL,
Firstname NVARCHAR(255) null,
Lastname NVARCHAR(255) null,
OrderId INT null,
OrderDate DATETIME null,
primary key (CustomerId)
)
create table Product (
ProductId INT IDENTITY NOT NULL,
Name NVARCHAR(50) not null,
Cost REAL not null,
primary key (ProductId)
)
alter table [Order]
add constraint FK_CustomerOrders
foreign key (Customer)
references Customer
alter table OrderProduct
add constraint FK_ProductOrders
foreign key (Product)
references Product
alter table OrderProduct
add constraint FK_OrderProducts
foreign key ([Order])
references [Order]
1 passed, 0 failed, 0 skipped, took 13.13 seconds (NUnit 2.5.1).
本文部分内容摘引自: YJingLee's Blog
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_CustomerOrders]') AND parent_object_id = OBJECT_ID('[Order]'))
alter table [Order] drop constraint FK_CustomerOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_ProductOrders]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_ProductOrders
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FK_OrderProducts]') AND parent_object_id = OBJECT_ID('OrderProduct'))
alter table OrderProduct drop constraint FK_OrderProducts
if exists (select * from dbo.sysobjects where id = object_id(N'[Order]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [Order]
if exists (select * from dbo.sysobjects where id = object_id(N'OrderProduct') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table OrderProduct
if exists (select * from dbo.sysobjects where id = object_id(N'Customer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Customer
if exists (select * from dbo.sysobjects where id = object_id(N'viewCustomer') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table viewCustomer
if exists (select * from dbo.sysobjects where id = object_id(N'Product') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Product
create table [Order] (
OrderId INT IDENTITY NOT NULL,
OrderDate DATETIME not null,
Customer INT not null,
primary key (OrderId)
)
create table OrderProduct (
[Order] INT not null,
Product INT not null
)
create table Customer (
CustomerId INT IDENTITY NOT NULL,
Version INT not null,
Firstname NVARCHAR(50) null,
Lastname NVARCHAR(50) null,
primary key (CustomerId),
unique (Firstname, Lastname)
)
create table viewCustomer (
CustomerId INT IDENTITY NOT NULL,
Firstname NVARCHAR(255) null,
Lastname NVARCHAR(255) null,
OrderId INT null,
OrderDate DATETIME null,
primary key (CustomerId)
)
create table Product (
ProductId INT IDENTITY NOT NULL,
Name NVARCHAR(50) not null,
Cost REAL not null,
primary key (ProductId)
)
alter table [Order]
add constraint FK_CustomerOrders
foreign key (Customer)
references Customer
alter table OrderProduct
add constraint FK_ProductOrders
foreign key (Product)
references Product
alter table OrderProduct
add constraint FK_OrderProducts
foreign key ([Order])
references [Order]
1 passed, 0 failed, 0 skipped, took 13.13 seconds (NUnit 2.5.1).
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。