Another way to solve this problem seems to be to create an
IResultTransformer that takes the first object in each row tuple:
public class FirstTupleResultTransformer :
NHibernate.Transform.IResultTransformer
{
public System.Collections.IList TransformList
(System.Collections.IList collection)
{
return collection;
}
public object TransformTuple(object[] tuple, string[] aliases)
{
return tuple[0];
}
}
Add this to the query with SetResultTransformer, and then
q.List<PatientIntensity>() works as I expect.
For my purposes, it is sufficient to disregard the 1-Nth tuple objects
in each row (more objects if more joins are added), and it'll throw an
error if the first tuple returned by my query is not of the correct
type, which is fine and dandy.
Just seems like this is what should have happened by default with
q.List<T>() rather than requiring one to do this explicitly.
Still interested to hear from any others who have been this way....
On Feb 14, 11:05 am, rsr <[email protected]> wrote:
> Having a problem getting eager fetching of an association property to
> work smoothly,
> I have an entity called PatientIntensity with an association property
> PatientStay.
>
> A simple attempt to query in code:
> var q = es.Session.CreateSQLQuery(@"
> select {pi.*}, {ps.*}
> from patientintensity pi
> join patientstay ps on ps.patientstayid = pi.patientstayid")
> .AddEntity("pi", typeof(PatientIntensity))
> .AddJoin("ps", "pi.PatientStay");
>
> Now, I would think that I can do
> var list = q.List<PatientIntensity>();
>
> but when I do that, I get an error:
> System.ArgumentException : The value "System.Object[]" is not of type
> "PatientIntensity" and cannot be used in this generic collection.
> Parameter name: value
> at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object
> value, Type targetType)
> at System.Collections.Generic.List`1.VerifyValueType(Object value)
> at System.Collections.Generic.List`1.System.Collections.IList.Add
> (Object item)
> at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from)
> at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery
> customQuery, QueryParameters queryParameters, IList results)
> at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec,
> QueryParameters queryParameters, IList results)
> at NHibernate.Impl.SessionImpl.List[T](NativeSQLQuerySpecification
> spec, QueryParameters queryParameters)
> at NHibernate.Impl.SqlQueryImpl.List[T]()
>
> However, I can get it to work, including join fetching the
> association, by doing the following;
> var l = q.List<object[]>();
> var l2 = l.Select(x => x[0] as PatientIntensity).ToList();
>
> Basically, take the first object in the array for each row and return
> it as a list.
>
> Is this what we would expect, or am I missing some way to get NH to do
> this for me? I note that all of the documentation examples I find seem
> to be using the non-generic List() method rather than the generic
> List<T> method, so maybe this is just how things are?
>
> Thanks
> roger
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---