如何在nhibernate中保存动态对象 [英] How to save a dynamic object in nhibernate

查看:194
本文介绍了如何在nhibernate中保存动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有User表,并且我创建了Nhibernate映射来从该表中获取数据.我创建了动态映射,该映射返回哈希表列表以响应用户类型,而不是用户类型,因为用户不存在物理类.我用于获取和保存数据的代码如下:

dynamic user = null;
using (ISession session = factory.OpenSession())
{
    user = session.CreateQuery("select u from User as u").List();

    user[0]["LastName"] = "s";
    session.Save(user[0]);
    session.Flush();
}

using (ISession otherSession = factory.OpenSession())
{
    user[0]["LastName"] = "ssss";
    otherSession.Save(user[0]);
    otherSession.Flush();
}

现在,在第一种情况下,当我获取数据并将其保存在同一会话中时,它可以工作.但是在第二种情况下,当我在一个会话中获取数据,然后在另一个会话中保存相同的对象时,它将不起作用.我收到错误:没有持久性:System.Collections.Hashtable".我现在认为这是一种奇怪的要求,但是如果有人知道我可以实现的任何方式,我将很感激.

我对User表的nhibernate映射如下:

<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2' >
   <class entity-name='User' table='`User`'>
      <id name='UserId' column='`UserId`' type='string'>
        <generator class='identity'>
        </generator>
      </id>
      <property name='CreatedOn' column='`CreatedOn`'  type='DateTime' />
      <property name='FirstName' column='`FirstName`' type='string' />
      <property name='LastName' column='`LastName`' type='string' />
      <property name='LastUserNewResultAcknowledgedTime' column='`LastUserNewResultAcknowledgedTime`' type='string' />
      <property name='LoginName' column='`LoginName`' type='string' />
      <property name='LoginPassword' column='`LoginPassword`' type='string' />
      <property name='ModifiedBy' column='`ModifiedBy`' type='string'/>
      <property name='ModifiedOn' column='`ModifiedOn`' type='DateTime' />
   </class>
</hibernate-mapping>" 

解决方案

似乎您所做的一切都正确.映射似乎可以((在第一种情况下已得到证明).因此,您的动态模型是可以的.在此处查看更多信息:

http://nhibernate.info/doc/nh/zh/index.html#persistent-classes-dynamicmodels

可能缺少的内容(代码段中未显示)是明确切换到dynamic-map的内容.这可以在工厂配置中完成,或者在使用显式参数 EntityMode.Map :

进行会话时完成.

using (ISession otherSession = factory.OpenSession(EntityMode.Map))
{
 ...
}

因为如果会话期望POCO(存在User类存在)并且提供了Hashtable(来自上一个会话,因此现在是真正的分离对象),则它无法为其找到持久性. /p>

I have User table in database and I created Nhibernate mapping for fetching data from that table. I have created dynamic mapping that returns list of hashtables in response not the User type because there is no physical class exists for User. My code for fetching and saving data is as follows:

dynamic user = null;
using (ISession session = factory.OpenSession())
{
    user = session.CreateQuery("select u from User as u").List();

    user[0]["LastName"] = "s";
    session.Save(user[0]);
    session.Flush();
}

using (ISession otherSession = factory.OpenSession())
{
    user[0]["LastName"] = "ssss";
    otherSession.Save(user[0]);
    otherSession.Flush();
}

Now, in the first case when I fetch the data and save it in the same session, it works. But in the second case when I fetch the data in one session and then save the same object in another session it does not work. I get the error: "No persister for: System.Collections.Hashtable". I now that its a kind of weird requirement but if someone knows any way I can achieve it I will be thankful.

My nhibernate mapping for User table is as follows:

<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2' >
   <class entity-name='User' table='`User`'>
      <id name='UserId' column='`UserId`' type='string'>
        <generator class='identity'>
        </generator>
      </id>
      <property name='CreatedOn' column='`CreatedOn`'  type='DateTime' />
      <property name='FirstName' column='`FirstName`' type='string' />
      <property name='LastName' column='`LastName`' type='string' />
      <property name='LastUserNewResultAcknowledgedTime' column='`LastUserNewResultAcknowledgedTime`' type='string' />
      <property name='LoginName' column='`LoginName`' type='string' />
      <property name='LoginPassword' column='`LoginPassword`' type='string' />
      <property name='ModifiedBy' column='`ModifiedBy`' type='string'/>
      <property name='ModifiedOn' column='`ModifiedOn`' type='DateTime' />
   </class>
</hibernate-mapping>" 

解决方案

It seems that you are doing everything correctly. Mapping seems to be OK (and that has been prooven in the first scenario). So your dynamic model is OK. see more here:

http://nhibernate.info/doc/nh/en/index.html#persistent-classes-dynamicmodels

What could be missing (it is not shown in your snippet) is explicit switch to dynamic-map. It could be done in configuration of your factory or when you are getting a session with explicit parameter EntityMode.Map:

using (ISession otherSession = factory.OpenSession(EntityMode.Map))
{
 ...
}

Because if the session is expecting the POCO (that there is User class exists) and is provided with Hashtable (coming from previous session ,so now really detached object), it cannot find the persister for it.

这篇关于如何在nhibernate中保存动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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