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?
, TupleToPropertyResultTransformer . , , SQL- , TupleToPropertyResultTransformer.
, , .. , TupleToPropertyResultTransformer SQL- NHibernate.
:
query.SetResultTransformer(Transformers.AliasToBean(typeof(Person)));
Person .
, , ?
, , , , . ( , , , ).
, "" "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 .
, NHibernate , , , Person, . , NHibernate , , - , xml.
, ...
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.
Source: https://habr.com/ru/post/1704999/More articles:Не регулярный контекстно-свободный язык и бесконечные регулярные подъязыки - regexCan you combine replace_html with visual_effect in Rails Rails? - javascriptShould a PHP application handle error handling with invalid database values? - databaseМой код правильно очищает свой список? - c#"Element" - invalid XmlNodeType - c #Why is my WPF track animation flickering? - animationHow to implement cross-domain Ajax query using CakePHP and jQuery? - jqueryIs there an HTML version of the ECMA-335 CLI specification? - command-line-interfaceDeployable content in a Visual Studio 2005 project ... how? - build-processHow to create an interface similar to Excel spreadsheet on iPhone? - user-interfaceAll Articles