title: NHibernate joined-subclass error - Object does not match target type
body: <p>I'm getting an error when I try to add a object to the database using joined-subclass with NHibernate. Here's my code:</p>
  
  <p>Pessoa.cs</p>
  
  <pre><code>namespace CarvalhoRodrigues.Domain.Cadastro
  {
      public class Pessoa
      {
          public Pessoa()
          {
              this.Endereco = new List&lt;Endereco&gt;();
          }
  
          public virtual long Id { get; set; }
          public enum TipoPessoa { Fisica, Juridica }
          public virtual TipoPessoa Tipo { get; set; }
          public virtual ICollection&lt;Endereco&gt; Endereco { get; set; }
  
      }
  }
  </code></pre>
  
  <p>PessoaFisica.cs</p>
  
  <pre><code>namespace CarvalhoRodrigues.Domain.Cadastro
  {
      public class PessoaFisica : Pessoa
      {
          public virtual string CPF { get; set; }
          public virtual string Nome { get; set; }
          public virtual DateTime DataNascimento { get; set; }
      }
  }
  </code></pre>
  
  <p>PessoaJuridica.cs</p>
  
  <pre><code>namespace CarvalhoRodrigues.Domain.Cadastro
  {
      public class PessoaJuridica
      {
          public virtual string CNPJ { get; set; }
          public virtual string RazaoSocial { get; set; }
          public virtual DateTime DataConstituicao { get; set; }
          public virtual string NomeFantasia { get; set; }
          public virtual ICollection&lt;Pessoa&gt; Representantes { get; set; }
      }
  }
  </code></pre>
  
  <p>PessoaRepository.cs</p>
  
  <pre><code>namespace CarvalhoRodrigues.Domain.Repositories.Cadastro
  {
      class PessoaRepository : IPessoaRepository
      {
          public void Inserir(Pessoa Pessoa)
          {
              using (ISession session = NHibernateHelper.OpenSession())
              {
                  using (ITransaction transaction = session.BeginTransaction())
                  {
                      session.Save(Pessoa);
                      transaction.Commit();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Pessoa.hbm.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                 assembly="CarvalhoRodrigues.Domain"
                 namespace="CarvalhoRodrigues.Domain.Cadastro"&gt;
  
    &lt;class name="Pessoa"&gt;
      &lt;id name="Id"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;property name="Tipo" /&gt;
  
      &lt;bag name="Endereco" lazy="false"&gt;
        &lt;key column="PessoaId" /&gt;
        &lt;one-to-many class="Endereco" /&gt;
      &lt;/bag&gt;
  
      &lt;joined-subclass name="PessoaFisica"&gt;
        &lt;key column="PessoaId" /&gt;
        &lt;property name="CPF" /&gt;
        &lt;property name="Nome" /&gt;
        &lt;property name="DataNascimento" /&gt;
      &lt;/joined-subclass&gt;
  
      &lt;joined-subclass name="PessoaJuridica"&gt;
        &lt;key column="PessoaId" /&gt;
        &lt;property name="CNPJ" /&gt;
        &lt;property name="RazaoSocial" /&gt;
        &lt;property name="DataConstituicao" /&gt;
        &lt;property name="NomeFantasia" /&gt;
      &lt;/joined-subclass&gt;
  
    &lt;/class&gt;
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>The error:</strong></p>
  
  <p>TestCase 'CarvalhoRodrigues.Tests.GenerateSchema_Fixture.Can_add_pessoa'
  failed: NHibernate.PropertyAccessException : Exception occurred getter of CarvalhoRodrigues.Domain.Cadastro.Pessoa.Id
    ----> System.Reflection.TargetException : Objeto não coincide com o tipo de destino.
      em NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target)
      em NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor)
      em NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity, IIdentifierGenerator generator)
      em NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory)
      em NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory)
      em NHibernate.Persister.Entity.JoinedSubclassEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
      em NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg)
      em NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
      em NHibernate.Cfg.Configuration.BuildSessionFactory()
      D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\NhibernateHelper.cs(19,0): em CarvalhoRodrigues.Domain.NHibernateHelper.get_SessionFactory()
      D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\NhibernateHelper.cs(27,0): em CarvalhoRodrigues.Domain.NHibernateHelper.OpenSession()
      D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\Repositories\Cadastro\PessoaRepository.cs(35,0): em CarvalhoRodrigues.Domain.Repositories.Cadastro.PessoaRepository.Inserir(Pessoa Pessoa)
      D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\Tests\PessoaTests.cs(47,0): em CarvalhoRodrigues.Tests.GenerateSchema_Fixture.Can_add_pessoa()
      --TargetException
      em System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
      em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
      em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
      em System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
      em NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target)</p>
  
  <p>"Objeto não coincide com o tipo de destino" does mean "Object does not match target type" in Portuguese. I can't figure out what I did wrong. The <strong>exact same</strong> code was working before I added the second joined-subclass in the Pessoa.hbm.xml with the mapping to PessoaJuridica.cs, then I put that mapping there and started getting this error.</p>