Wednesday, October 24, 2007

NHibernate AliasToBean Transformations

NHibernate's AliasToBean result transformer looks very useful. It allows you to map to unmanaged entities (don't ask why we have some of these!)

If you look at the docs you can see how this is supposed to work. However when I tried to implement this I kept receiving a "NHibernate.QueryException: Return types of SQL query were not specified". To workaround this I had to use the AddScalar() method to define the return types which is actually what I was trying to avoid by using the transformer:

ISQLQuery q =_session.CreateSQLQuery("SELECT c1 ID FROM t1 i WHERE c2 = :value1");
q.SetInt64("value1", value1);
q.SetResultTransformer(Transformers.AliasToBean(typeof(UnmanagedEntity)));
q.AddScalar("ID", NHibernateUtil.String);
return (List< UnmanagedEntity >)q.List< UnmanagedEntity >();

I'll post this on the forums to determine whether my problem is due to a bug or a misunderstanding on my part.

0 Comments:

Post a Comment

<< Home