如何在NH 3.2中实现具有多对多关系的.ChildWhere()映射

时间:2012-01-04 19:20:38

标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping conform

我有以下FNH映射:

public class ItemMap : ClassMap<Item>
{
   public ItemMap ()
   {
       this.HasManyToMany(a => a.ChildItems).ChildWhere("IsDeleted = 0").AsSet();
   }
}

结果hbm文件是:

<hibernate-mapping>
  <class name="Item" table="Item">
    <set name="ChildItems" table="ItemsToChildItems">
      ...
      <many-to-many class="ChildItem" where="IsDeleted = 0">
        <column name="ChildItemId" />
      </many-to-many>
    </set>
  </class>
</hibernate-mapping>

我想使用“性能映射代码功能实现相同的映射:-)/ conformability方法”的NHibernate 3.2

注意: 以下方法不起作用:

public class ItemMap : ClassMapping<Item>
{
   public ItemMap()
   {
      this.Set(x => x.ChildItems
      , map =>
      {
         map.Where("IsDeleted = 0");
      }
      , action => action.ManyToMany());
   }
}

因为: 它遵循FNH映射:

public class ItemMap : ClassMap<Item>
{
   public ItemMap ()
   {
      this.HasManyToMany(a => a.ChildItems).Where("IsDeleted = 0").AsSet();
   }
}

.Where(“IsDeleted = 0”)和.ChildWhere(“IsDeleted = 0”)不一样。

HBM差异:

使用.ChildWhere("IsDeleted = 0")的结果hbm文件是:

<hibernate-mapping>
  <class name="Item" table="Item">
    <set name="ChildItems" table="ItemsToChildItems">
      ...
      <many-to-many class="ChildItem" where="IsDeleted = 0">
        <column name="ChildItemId" />
      </many-to-many>
    </set>
  </class>
</hibernate-mapping>

使用.Where("IsDeleted = 0")的结果hbm文件是:

<hibernate-mapping>
  <class name="Item" table="Item">
    <set name="ChildItems" table="ItemsToChildItems" where="IsDeleted = 0">
      ...
      <many-to-many class="ChildItem">
        <column name="ChildItemId" />
      </many-to-many>
    </set>
  </class>
</hibernate-mapping>

谁有类似的问题或可以提供解决方案?需要帮忙。

1 个答案:

答案 0 :(得分:2)

我有同样的问题,我找到了解决方案:

https://nhibernate.jira.com/browse/NH-2997

分3步解决:

1)向IManyToManyMapper接口添加新的接口方法:

public interface IManyToManyMapper : IColumnsMapper {
void Where(string sqlWhereClause); }

2)在ManyToManyCustomizer类中实现新方法:

public void Where(string sqlWhereClause) { customizersHolder.AddCustomizer(propertyPath, (IManyToManyMapper x) => x.Where(sqlWhereClause)); }

3)在ManyToManyMapper类中实现新方法:

public void Where(string sqlWhereClause) { manyToMany.where = sqlWhereClause; }