2

我有以下FNH映射:如何實現.ChildWhere()映射與許多一對多的關係,在NH 3.2

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

。凡( 「請將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> 

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

誰也有類似的問題,或可提供解決方案?需要幫忙。

回答

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