2

当我使用包含由“Bag”映射的“IList<>”的对象调用我的 DAO 的两次 Save() 时,出现异常“非法尝试将集合与两个打开的会话相关联”:

public abstract class AbstractGenericDao<TEntity> : HibernateDaoSupport, IAbstractGenericDao<TEntity>
{
  [Transaction]
  public TEntity Save( [NotNull] TEntity entity )
  {
    HibernateTemplate.Save( entity );
  }
}

我正在使用两个 SessionFactories

<!-- NHibernate Configuration -->
<object id="NHibernateSessionFactory" abstract="true" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
  <property name="HibernateProperties">
    <dictionary>
      <entry key="connection.provider" value="${connection.provider}" />
      <entry key="connection.driver_class" value="${hibernate.driver}" />
      <entry key="dialect" value="${hibernate.dialect}" />
      <entry key="hbm2ddl.auto" value="${hibernate.hbm2ddl}" />
      <entry key="show_sql" value="${hibernate.show_sql}" />
    </dictionary>
  </property>
</object>

<!-- NHibernate Session Configuration -->
<object id="NHibernateSessionFactory_1" parent="NHibernateSessionFactory" >
  <property name="DbProvider" ref="DbProvider_Main" />
  <property name="MappingAssemblies">
    <list>
      <value>DomainModel</value>
    </list>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

<!-- NHibernate Session Configuration -->
<object id="NHibernateSessionFactory_2" parent="NHibernateSessionFactory" >
  <property name="DbProvider" ref="DbProvider_Exam" />
  <property name="MappingAssemblies">
    <list>
      <value>DomainModel</value>
    </list>
  </property>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

和“TxScopeTransactionManager”:

<object id="transactionManager" type="Spring.Data.Core.TxScopeTransactionManager, Spring.Data">
    <property name="TransactionSynchronization" value="Always"/>
</object>

有谁能够帮我?

4

0 回答 0