2011-04-18 30 views
7

我有這個實體的<dynamic-component>條目和一些 屬性。它被作爲 IDictionary在實體類中消耗。NHibernate 3,動態組件,字典和LINQ查詢

映射工作正常,一切都是hunky dory直到 我去根據這個字典中的值進行查詢。

首先我嘗試以下LINQ查詢:

repository.Where(x => x.Specifications[key] == value) 

要查詢反對。 (規格是動態組件)的 查詢導致以下錯誤:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'NHibernate.Type.ComponentType' to type 'NHibernate.Type.CollectionType'.

搞清楚這可能是LINQ提供程序然後我 繼續打造BaseHqlGeneratorForMethod處理自定義LINQ 擴展的邊界之外爲了它。

var specificationExpression = 
    treeBuilder.Dot( 
    visitor.Visit(arguments[0]).AsExpression(), 
    treeBuilder.Ident("Specifications")).AsExpression(); 
var targetExpression = 
    treeBuilder.Dot( 
    visitor.Visit(arguments[0]).AsExpression(), 
    treeBuilder.Ident(keyExpression.Value.ToString())).AsExpression(); 

這生成正確的SQL除了 表達緩存,以便後續調用該函數將 比較所有值偉大的工作:

它是通過使用treeBuilder.Dot(...) AST這樣建立起來的針對第一把鑰匙。

在這裏,我找到了treeBuilder.DictionaryItem(...) AST節點和 建成了以下內容:

var specificationExpression = 
    treeBuilder.Dot( 
    visitor.Visit(arguments[0]).AsExpression(), 
    treeBuilder.Ident("Specifications")).AsExpression(); 
var specification = 
    treeBuilder.DictionaryItem(specificationExpression, key).AsExpression(); 

我再一次遭到了以下錯誤:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'NHibernate.Type.ComponentType' to type 'NHibernate.Type.CollectionType'.


問題

我在這裏做錯了什麼? <dynamic-component>不能被查詢嗎?我是否錯誤地執行了這個?這可能是我應該報告的錯誤嗎?

映射:

<dynamic-component name="Specifications"> 
    <property name="sp_Graphics" column="sp_Graphics" /> 
    <property name="sp_Weight" column="sp_Weight" /> 
</dynamic-component> 

實體:

/// <summary> 
/// Specifications 
/// </summary> 
public virtual IDictionary Specifications { get; set; } 

回答

4

由於NHibernate的LINQ提供程序中的錯誤,你將無法使用它來查詢動態組件的3.1.0

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

這裏是希望修復可以爲版本3.2

與此同時,您應該使用Criteria或HQL查詢。