How to save a dynamic object in nhibernate

风格不统一 提交于 2019-12-12 00:57:41

问题


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>" 

回答1:


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.



来源:https://stackoverflow.com/questions/13284923/how-to-save-a-dynamic-object-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!