Dynamic Instantiation class with ImportType
Hi All,
I'm trying to use a 'dynamic instantiation class' as outlined in Nhibernate In Action, Chpt 7 - Writing Report Queries. I have an HQL query where I'd like to use this (this is a simplification, the real query will be much more complex):
var query = Session.CreateQuery(
@"select new PatientVisitSummary(v.id, v.AdmissionDate)
from Patient p join p.Visits v
where p.Id = :patientId");
I've created a mapping file for this class:
public class PatientVisitSummaryMap : ClassMap<PatientVisitSummary>
{
public PatientVisitSummaryMap()
{
ImportType<PatientVisitSummary>();
}
}
However, when I try to execute the query, I receive the following exception:
NHibernate.MappingException: (XmlDocument)(3,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.
I've tried to follow the guidance I've seen in out fluent-nhibernate posts on the subject, but without success. Any advice / info on where I'm going wrong would be greatly appreciated.
Thx.
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by James Gregory on 04 Mar, 2010 01:17 PM
Paul replied to your question on the mailing list with this answer:
I'll expand upon his answer a bit. The
ImportTypemethod is used to "import" an unrelated class into NHibernate, so it recognises it when used in queries. The primary function of this is so you can project to a DTO in your queries, instead of returning the entity directly.Where you're going wrong in your mappings is that you're creating a mapping for your
PatientVisitSummaryclass instead of for yourPatientclass. You need to do the following:3 Posted by Jim Wheaton on 04 Mar, 2010 05:23 PM
Ah, that makes sense. Thanks for taking the time to reply.
James Gregory resolved this discussion on 05 Mar, 2010 09:48 AM.