Using Unmapped Class with NHibernate Named Query

I am using a custom query called NHibernate, which I want to return a collection of Person objects. The Person object does not map to the NHibernate mapping, which means that I get the following exception:

System.Collections.Generic.KeyNotFoundException: The specified key was not in the dictionary.

This is obtained when the session is created because it cannot find the class name when calling NHibernate.Cfg.Mappings.GetClass (String className). This is all clear, but I was wondering if there is a way to tell NHibernate to use the class, even if I don't have a mapping for it?

+3
source share
5 answers

, TupleToPropertyResultTransformer . , , SQL- , TupleToPropertyResultTransformer.

, , .. , TupleToPropertyResultTransformer SQL- NHibernate.

0

:

query.SetResultTransformer(Transformers.AliasToBean(typeof(Person)));

Person .

+9

, , ?

, , , , . ( , , , ).

, "" "DTO". "" Person, NHibernate, ResultTransformer.

- :

ICriteria crit = session.CreateCriteria (typeof(Person));

// set some filter criteria

crit.SetProjection (Projections.ProjectionList()
                     .Add (Property("Name"), "Name")
                     .Add (Property( ... )
                   );

crit.SetResultTransformer(Transformers.AliasToBean(typeof(PersonView));

return crit.List<PersonView>();

, , NHibernate .

+2

, NHibernate , , , Person, . , NHibernate , , - , xml.

0

, ...

However, you cannot use a named query to directly enter results in an unlabeled class. You will need to specify which columns to put in which fields or, in other words, the display .;) However, you can return scalar values ​​from a named query, and you can take these arrays of objects and create your collection manually.

0
source

Source: https://habr.com/ru/post/1704999/


All Articles