Fluentnhibernate is mounting the wrong query

Asked

Viewed 44 times

2

Dear,

After mapping the tables, I consulted one of them to return the information "daughter". The sql was mounted correctly, however the name of the key column (PK) was changed which made an error in the execution of the script. The script is correct, but the name of the pk column of the person table was misspelled. See:

CONSULTATION

var partePessoas = (from pdoc in Sessao.Query<PessoaDocumento>()
                    let documento = pdoc.NumDocPess.Trim()
                    where documento.Equals(numDocumento)
                    select pdoc.Pessoa.Parte).ToList().Distinct();

Script mounted:

select parte2_.CodParte     as col_0_0_,
       parte2_.CodParte     as col_1_0_,
       pessoadocu0_.CodPess as col_2_0_,
       parte2_.CodParte     as Cod1_1974_,
       parte2_.ClassId      as Cla2_1974_,
       parte2_.CodDoc       as Cod3_1974_,
       parte2_.CodPess      as Cod4_1974_,
       parte2_.NumOrd       as Num5_1974_,
       parte2_.Pessoa_id    as Pes6_1974_
  from PESSOADOCUMENTO pessoadocu0_
  left outer join PESSOA pessoa1_ on pessoadocu0_.Pessoa_id = pessoa1_.CodPess
  left outer join PARTE parte2_ on pessoa1_.CodPess = parte2_.Pessoa_id
where trim(both from(RetiraMascaraCpfCnpj(pessoadocu0_.NumDocPess))) = '00000000000'

However, where it appears 'parte2_.Personal' should be 'parte2_.Codpess', as mapped below:

PERSON MAPPING

namespace Modelo.Mapas.Esparta
{
    public class PessoaMapa : ClassMap<Pessoa>
    {
        public PessoaMapa()
        {
            Table("PESSOA");
            Id(u => u.CodPess).Column("CodPess");
            Map(u => u.ClassId);
            Map(u => u.CodTipPerso);
            Map(u => u.IndAtivo).CustomType<Util.ActiveBoolType>();
            Map(u => u.Nome);
            Map(u => u.NomeFon);
            Map(u => u.NomeMai);
            HasMany(x => x.Documento);
            HasMany(x => x.Parte);
            HasMany(x => x.Advogado);                
        }
    }
}

MAPPING OF PEOPLE

namespace Modelo.Mapas.Esparta
{
    public class PessoaDocumentoMapa : ClassMap<PessoaDocumento>
    {
        public PessoaDocumentoMapa()
        {
            Table("PESSOADOCUMENTO");
            Id(p => p.CodPess);
            Map(p => p.CodPessDoc);
            Map(p => p.CodTipDocPess);
            Map(p => p.NumDocPess).Formula("(Esparta2.RetiraMascaraCpfCnpj(NumDocPess))");
            Map(p => p.Cpf).Formula("(Decode(CodTipDocPess, 1, Esparta2.RetiraMascaraCpfCnpj(NumDocPess), ''))");
            Map(p => p.Cnpj).Formula("(Decode(CodTipDocPess, 2, Esparta2.RetiraMascaraCpfCnpj(NumDocPess), ''))");
            References(p => p.Pessoa);
        }
    }
}

MAPPING OF PART

namespace Modelo.Mapas
{
    public class ParteMapa : ClassMap<Parte>
    {
        public ParteMapa()
        {
            Table("PARTE");
            Id(u => u.CodParte);
            Map(u => u.ClassId);
            Map(u => u.CodDoc);
            Map(u => u.CodPess);
            Map(u => u.NumOrd);
            HasMany(u => u.ParteProcesso);
            References(u => u.Pessoa);
        }
    }
}
  • "whereparte2_.Personal' should be 'parte2_.Codpess'"; Please edit the question and include the mapping of "Parte2" (in this case, table PARTE)

1 answer

0


After searching, find out that you need to specify the column name in the mapping, thus:

public PessoaMapa()
{
   Table("PESSOA");
   (...)
   HasMany(x => x.Documentos).KeyColumn("CodPess");
   HasMany(x => x.Partes).KeyColumn("CodPess"); 
   HasMany(x => x.Advogados).KeyColumn("CodPess");
 }

And so:

public PessoaDocumentoMapa()
        {
            Table("ESPARTA2.PESSOADOCUMENTO");
            (...)
            References(p => p.Pessoa, "CodPess").ForeignKey("CodPess");
        }

This way the query was built correctly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.