Rua*_*riQ 16 c# asp.net nhibernate spring.net nhibernate-mapping
目前的方法
在ASP.NET Web表单应用程序(使用Spring.NET和NHibernate)中,我们有一个聚合根(Person),其详细信息可以在多个屏幕/页面中捕获.的人在进入到该工作流的实体存在,并且对所有变更人对象图是原子的,因此只应在提交最终屏幕的刷新到数据库.
为了实现这一点,我们首次使用NHibernate 3.2将数据库中的Person(懒惰)加载到第一页,然后在我们浏览过程时加载并将序列化的Person对象图保存到HTTP Session变量中.
检索后的人出了HTTP会话,它是从当前的NHibernate会话的分离状态,所以我们重新连接通过调用更新()在当前会话的方法,就像这样:
var sessionPerson = Session[PersonSessionName] as Person;
var currentSession = SessionFactory.GetCurrentSession();
currentSession.Update(sessionPerson);
注意:使用Lock()引发异常,建议"重新关联的对象有脏集".
重新连接时,我们可以按预期遍历对象图,从数据库中提取尚未加载到内存中的子实体的数据.
映射文件的子集
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" assembly="Domain" namespace=" TestApp.Domain">
  <class name="Person" table="Person">
    <id name="Id">
      <generator class="TestApp.CustomNHibernateHiLoGenerator, TestApp.Core" />
    </id>
    <property name="Name" not-null="false" />
    <bag name="PersonCountries" access="field.camelcase-underscore" cascade="all-delete-orphan">
      <key column="PersonId" foreign-key="FK_ PersonCountry_Person" not-null="true" />
      <one-to-many class="PersonCountry" />
    </bag>
  </class>
  <class name="Country" table="Country">
    <id name="Id">
      <generator class="TestApp.CustomNHibernateHiLoGenerator, TestApp.Core" />
    </id>
    ... No back reference to Person
  </class>
</hibernate-mapping>
域
public class PersonCountry : Entity, ICloneable
{
    // No properties of note
}
public class Person : Entity, ICloneable
{
    public virtual string Name { get; set; }
    public virtual IEnumerable<PersonCountry> PersonCountries { get; set; }
    ... 
    // More Properties
}
刷新对数据库的更改
.. // Code-behind
PricingService.Save(ProductContext.Pricing, forceMerge: true);            
public class PricingService : IPricingService
{
   [Transaction]  // Spring.NET transaction
   public Pricing Save(Pricing pricing, bool forceMerge = false)
   {            
      if(forceMerge)
      {
         CurrentSession.Merge(entity);
      }
      else
      {
         CurrentSession.SaveOrUpdate(entity);
      }
   }
}
当需要刷新对数据库的所有更改时,如果我们只更改Name,则更改将按预期工作.然而,添加一个新的国家项目的人引起的级联合并()在一个一对多的关系,失败,出现以下异常(奇怪的是,除去国家工作正常).
NHibernate.StaleStateException: Batch update returned unexpected row count from update; actual row count: 0; Expected: 1
任何帮助将不胜感激.
| 归档时间: | 
 | 
| 查看次数: | 3117 次 | 
| 最近记录: |