2

我有以下 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>

我想使用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 差异:

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

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

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

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

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

4

1 回答 1

2

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

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

分三步解决:

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; }

于 2012-03-16T14:39:43.863 回答