Dynamic Instantiation class with ImportType

Original Post Jim Wheaton's Avatar

Jim Wheaton

26 Feb, 2010 05:16 PM via web

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.

  1. Support Staff 2 Posted by James Gregory on 04 Mar, 2010 01:17 PM

    James Gregory's Avatar

    Paul replied to your question on the mailing list with this answer:

    You don't want to create a map for PatientVisitSummary. You want to import PatientVisitSummary into one of the mapped classes, such as Patient or Visit. So don't create a PatientVisitSummaryMap, instead call ImportType from your Patient or Visit map.

    I'll expand upon his answer a bit. The ImportType method 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 PatientVisitSummary class instead of for your Patient class. You need to do the following:

    public class PatientMap : ClassMap<Patient>
    {
      public PatientMap()
      {
        ImportType<PatientVisitSummary>();
    
        // other mappings for Patient
      }
    }
    
  2. 3 Posted by Jim Wheaton on 04 Mar, 2010 05:23 PM

    Jim Wheaton's Avatar

    Ah, that makes sense. Thanks for taking the time to reply.

  3. James Gregory resolved this discussion on 05 Mar, 2010 09:48 AM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM