views:

334

answers:

2

If I have a mapping like this:

<class name="Library" table="Libraries">
  ...
  <dynamic-component name="Annotations">
    <property name="LibraryResolver.AlgorithmVersion" column="`LibraryResolver.AlgorithmVersion`" type="Int32" />
  </dynamic-component>
</class>

How should I write HQL or Linq-to-NHibernate query for all libraries where LibraryResolver.AlgorithmVersion is greater than a given value?

+1  A: 

I don't know whether this helps but when I use the Criteria API (in Java) it just works. Haven't tried with HQL though.

<dynamic-component name="values">
        <property name="dynamicNameValue" column="ATTRIBUTE_1" type="string"/>
        <property name="dynamicNumber" column="ATTRIBUTE_4" type="integer"/>
</dynamic-component>

Criteria criteria = session.createCriteria(DynamicAttributes.class)
                .add(Expression.eq("values.dynamicNumber", 2));

Just some thought: could it be that the problem is that the name ('LibraryResolver.AlgorithmVersion') you're passing contains a dot? Maybe post some code you already attempted?

David
Ok, I'll post the code a bit later, however I can already say that you are absolutely correct about the name. No idea how to escape or use it in this case. Criteria API works, but this query is issued from a library that does not even link to NHibernate, and I would like to leave it that way.
Andrey Shchekin
+1  A: 

The HQL query below maybe along the lines you are looking for

from Library as lib
where lib.Annotations.LibraryResolver.AlgorithmVersion > 2

If you're using nhibernate, have you tried out the NHibernate LambdaExtensions? This library provides a set of extensions methods over the Criteria and DetachedCriteria apis which removes the need of magic strings when querying using the above two api.

Below is an example of how one might use NHibernate Detached Criteria query with the mentioned LambdaExtensions library

Answer answerAlias = null;
var actual = DetachedCriteria.For<Survey>()
.Add<Survey>( s => s.Status == SurveyStatus.Complete )
.Add<Questionnaire>( q => q.Id == questionnaireId )
.CreateAlias<Survey>( s => s.Answers, () => answerAlias )
.SetProjection( LambdaProjection.Property( () => answerAlias.Id ) );
Noel
Does it work? I am certain I tried similar query and it assumed AlgorithmVersion is a subproperty. Unfortunately .NET in my system got corrupted which prevents me from trying this in the remaining hour till approval.Lambda extensions look interesting. I tried Linq-to-Nhibernate and it failed to create a reasonable query, however the extensions seem to provide more direct approach to building queries. On the other hand, I still do not want to reference NH from Domain Services.
Andrey Shchekin
Personally I've both used HQL and Criteria/Detached Criteria. I would recommend that you give the Lambda extensions a try. It definitely has helped me come up to speed quicker on the Criteria api.As for where the HQL statement would work or not, that would be something that you'll have to try out. But I'm pretty sure it will be a good starting point to start from :)
Noel