流利的NHibernate - 用复合键保存实体 [英] Fluent NHibernate -- Saving Entity with Composite Key

查看:147
本文介绍了流利的NHibernate - 用复合键保存实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有下面的表格:

pre $ CREATE TABLE CustomerHub(
CustomerId INT NOT NULL,
HubId INT NOT NULL

我已经映射到这个实体: / p>

  public class CustomerHub 
{
public int CustomerId {get; set;}
public int HubId {get; set}

// GetHashCode,Equals等...
}

使用此映射:

  public class CustomerHubMap:ClassMap< CustomerHub> 
{
UseCompositeId()
.WithKeyProperty(x => x.CustomerId)
.WithKeyProperty(x => x.HubId);





我遇到的问题是当我创建一个CustomerHub类型的新实体,试图保存它,没有什么是持久的数据库。我能够检索一切正常,只是不保存它们。例如:

  //这将会工作
var x = session.CreateCriteria(typeof(CustomerHub));

//这不会是
使用(var trans = session.BeginTransaction())
{
var custHub = new CustomerHub {CustomerId = 293,HubId = 1193 };
var y = session.SaveOrUpdate(custHub);
trans.Commit();

$ / code>

奇怪的是没有抛出异常,它只是不做任何事情。我启动了NH Profiler(优秀的工具!),它看起来不是向数据库发出任何类型的插入命令。



想法?



注意:我知道这个表看起来很像一个连接表(因为它在技术上是这样的),最好在另一个实体(如Customer)上作为ManyToMany关系使用。但是由于一些非常棘手的数据设计(即没有Hub表),将连接表映射到实体并以这种方式使用它是非常简单的。

解决方案

编辑(放在顶部)



来自NHibernate的文档:


由于其固有特性,使用此生成器的实体不能通过ISession的SaveOrUpdate()方法保存。相反,如果通过调用ISession的Save()或Update()方法保存或更新对象,则必须明确指定NHibernate。


http:// www。 nhforge.org/doc/nh/en/index.html#mapping-declaration-id-assigned (5.1.4.7)

复合ID有发生器= )















你没有发出刷新或提交。您可能已经习惯了在保存时发出INSERT的身份,但是复合是由您的代码分配的,而不是由数据库分配的,所以您需要刷新/提交以进行插入。






  var custHub = new CustomerHub {CustomerId = 293,HubId = 1193}; 
var y = session.SaveOrUpdate(custHub);
session.Flush();

  using(var tx = session.BeginTransaction())
{
var custHub = new CustomerHub {CustomerId = 293,HubId = 1193};
var y = session.SaveOrUpdate(custHub);
tx.Commit();

$ / code $ / pre



另外,使用复合键会让你恨你的生活。很多东西和复合键的功能不一样,并不明显。基本上NHibernate可以做的工作少,所以你必须拿起懈怠。


First, I have the following table:

CREATE TABLE CustomerHub (
   CustomerId INT NOT NULL,
   HubId INT NOT NULL
)

Which I have mapped to the this entity:

public class CustomerHub
{
   public int CustomerId {get;set;}
   public int HubId {get;set}

   //GetHashCode, Equals, Etc...
}

Using this mapping:

public class CustomerHubMap : ClassMap<CustomerHub>
{
   UseCompositeId()
      .WithKeyProperty(x => x.CustomerId)
      .WithKeyProperty(x => x.HubId);
}

The problem I have is that when I create a new entity of type CustomerHub and attempt to save it, nothing is persisted to the database. I am able to retrieve everything fine, just not save them. For example:

//this will work
var x = session.CreateCriteria(typeof(CustomerHub)); 

//this will not
using (var trans = session.BeginTransaction()) 
{
   var custHub = new CustomerHub {CustomerId = 293, HubId = 1193};
   var y = session.SaveOrUpdate(custHub); 
   trans.Commit();
}

The odd part is that no exceptions are thrown, it simply just doesn't do anything. I fired up NH Profiler (excellent tool!) and it looks like it isn't issuing any type of insert command to the database.

Thoughts?

Note: I realize that this table looks much like a join table (because it technically is) and would best be served as a ManyToMany relationship on another entity such as Customer... but due to some extremely screwy data design (ie no Hub table) it is FAR simpler to map the join table to an entity and work with it that way.

解决方案

Edit (put at top)

quote from NHibernate docs:

Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate() method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.

http://www.nhforge.org/doc/nh/en/index.html#mapping-declaration-id-assigned (5.1.4.7)

Composite ID have generator = assigned.

This goes back to composite id = sucking :)


You aren't issuing a flush or commit. You are probably used to working with something like identity that issues an INSERT on save, but composite are assigned by your code, not the database, so you need a flush/commit for the insert to take place.


var custHub = new CustomerHub {CustomerId = 293, HubId = 1193};
var y = session.SaveOrUpdate(custHub);
session.Flush();

or

using(var tx = session.BeginTransaction())
{
    var custHub = new CustomerHub {CustomerId = 293, HubId = 1193};
    var y = session.SaveOrUpdate(custHub);
    tx.Commit();
}


Also, one note: using composite keys will make you hate your life. A lot of things don't work the same with composite keys and it isn't immediately obvious. Basically NHibernate can do less of the work so you have to pick up the slack.

这篇关于流利的NHibernate - 用复合键保存实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆