tags:

views:

619

answers:

1

I'm getting an error when I try to add a object to the database using joined-subclass with NHibernate. Here's my code:

Pessoa.cs

namespace CarvalhoRodrigues.Domain.Cadastro
{
    public class Pessoa
    {
        public Pessoa()
        {
            this.Endereco = new List<Endereco>();
        }

        public virtual long Id { get; set; }
        public enum TipoPessoa { Fisica, Juridica }
        public virtual TipoPessoa Tipo { get; set; }
        public virtual ICollection<Endereco> Endereco { get; set; }

    }
}

PessoaFisica.cs

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; }
    }
}

PessoaJuridica.cs

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<Pessoa> Representantes { get; set; }
    }
}

PessoaRepository.cs

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();
                }
            }
        }
    }
}

Pessoa.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="CarvalhoRodrigues.Domain"
               namespace="CarvalhoRodrigues.Domain.Cadastro">

  <class name="Pessoa">
    <id name="Id">
      <generator class="identity" />
    </id>
    <property name="Tipo" />

    <bag name="Endereco" lazy="false">
      <key column="PessoaId" />
      <one-to-many class="Endereco" />
    </bag>

    <joined-subclass name="PessoaFisica">
      <key column="PessoaId" />
      <property name="CPF" />
      <property name="Nome" />
      <property name="DataNascimento" />
    </joined-subclass>

    <joined-subclass name="PessoaJuridica">
      <key column="PessoaId" />
      <property name="CNPJ" />
      <property name="RazaoSocial" />
      <property name="DataConstituicao" />
      <property name="NomeFantasia" />
    </joined-subclass>

  </class>

</hibernate-mapping>

The error:

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)

"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 exact same 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.

+4  A: 

I may be wayyyy off here, but shouldn't PessoaJuridica derive from Pessoa or from PessoaFisica?

Simon Wilson
From Pessoa... BTW, the error occurs when I try to insert a new Pessoa, despite of the type, with repo.Inserir(p);, for example.
Alaor
Oh sorry, now I see, there's missing the inheritance, oh God! What a shame!
Alaor
Accepted the answer, really thankz man, some hours of code can make you blind, thank you for the light!
Alaor