NHibernate 3, dynamic components, dictionaries, and LINQ queries

I have this object with a <dynamic-component> entry and a number of properties on it. It is consumed by the entity class as an IDictionary .

Mapping works fine, and that’s all. I turn to a query based on the values ​​in this dictionary.

First I tried the following linq query:

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

To request it. (Specifications - dynamic component). The request resulted in the following error:

Unhandled exception: System.InvalidCastException: cannot discard an object of type "NHibernate.Type.ComponentType" to enter a type of 'NHibernate.Type.CollectionType.

Assuming that this could be outside the BaseHqlGeneratorForMethod Linq I provider, then he continued to build BaseHqlGeneratorForMethod to handle the custom linq extension for it.

It was created using treeBuilder.Dot(...) AST as follows:

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

This works fine to create the correct SQL, except that the expression is cached, so subsequent calls to this function will compare all values ​​with the first key.

From here I found treeBuilder.DictionaryItem(...) AST Node and built the following:

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

The following error met me again:

Unhandled exception: System.InvalidCastException: Cannot execute an object of type "NHibernate.Type.ComponentType" to enter a type of 'NHibernate.Type.CollectionType.


Question

What am I doing wrong here? Can <dynamic-component> not be requested? Am I implementing it wrong? Maybe this is a mistake that I should report?

Matching:

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

Essence:

 /// <summary> /// Specifications /// </summary> public virtual IDictionary Specifications { get; set; } 
+7
source share
2 answers

Due to an error in the linq NHibernate provider, you cannot use it to query dynamic components with 3.1.0

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

Here we hope that a fix can be worked out for version 3.2.

In the meantime, you should use a Criteria or HQL query.

+4
source

Fixed with NHibernate 3.3.1 (valid link here: https://nhibernate.jira.com/browse/NH-2664 )

+4
source

All Articles