Inheritance with 0: N Relationship using Nhibernate

1

I have the following situation:

Person

    public class Pessoa
{  
       public Pessoa()
       {
         Endereco = new List<Endereco>();  
        }    
    public virtual int IdPessoa { get; set; }
    public virtual string Nome { get; set; }
    public virtual IList<Endereco> Endereco { get; set; }
 }

Map

    public class PessoaMap: ClassMap<Pessoa>
{

    public PessoaMap()
    {

        Id(x => x.IdPessoa);

        Map(x => x.Nome)
            .Not.Nullable()
            .Length(MapLength.Texto);

        HasMany<Endereco>(x => x.Endereco)
        .KeyColumn("TipodePessoa")
        .Cascade.All()
        .Inverse();


        Table("Pessoa");


    }

Address

   public class Endereco:Pessoa
{



    public virtual string Numero { get; set; }
    public virtual string Complemento { get; set; }
    public virtual string CEP { get; set; }
    public virtual string Cidade { get; set; }
    public virtual string UF { get; set; }
    public virtual string Pais { get; set; }
    public virtual Pessoa Pessoa { get; set; }

}

Map

    public class PessoaEnderecoMap : SubclassMap<PessoaEndereco>
 {

    public PessoaEnderecoMap()
    {



        Table(@"PessoaEndereco");

        KeyColumn("IdPessoa");




         Map(x => x.Numero)
          .Not.Nullable()
          .Length(MapLength.TextoCurto);

         Map(x => x.Complemento)
         .Not.Nullable()
         .Length(MapLength.TextoCurto);

         Map(x => x.CEP)
         .Not.Nullable()
         .Length(MapLength.TextoCurto);

        Map(x => x.Cidade)
        .Not.Nullable()
        .Length(MapLength.TextoCurto);

        Map(x => x.UF)
       .Not.Nullable()
       .Length(MapLength.TextoCurto);

        Map(x => x.Pais)
       .Not.Nullable()
       .Length(MapLength.TextoCurto);

        References(x => x.Pessoa);

    }
}

CustomerCustom

    public class PessoaCliente : Pessoa
{  
    public virtual string Cnpj { get; set; }
    public virtual string InscrEstadual { get; set; }
    public virtual string Telefone { get; set; }

}

Map

 public class PessoaClienteMap : SubclassMap<PessoaCliente>
 {


    public PessoaClienteMap() {

        Table(@"PessoaCliente");

        KeyColumn("IdPessoa");

        Map(x => x.Cnpj)
         .Length(MapLength.TextoCurto);


        Map(x => x.InscrEstadual)
       .Length(MapLength.TextoCurto);

        Map(x => x.Telefone)
       .Length(MapLength.TextoCurto);


    }

 }

PersonLogin

   public class PessoaLogin:Pessoa
{

    public virtual string Senha { get; set; }
}

Map

    public class PessoaLoginMap : SubclassMap<PessoaLogin> 
{

    public PessoaLoginMap()
    {

        KeyColumn("IdPessoa");

        Map(x => x.Senha)
            .Not.Nullable()
            .Length(MapLength.TextoMini);


        Table("PessoaLogin");


    }


}

In this way, bothClient and PersonLogin are given Address. How do I

Does Customer have Address and PersonLogin data not?

I could take Address from the Person Parent Class, and do the relationship between Address and CustomerCustomer only that I will fall into another problem of double-column because this Address Class will be used also in PersonContact, PersonForm, PersonProfile that inherits from Person.

I wanted to leave Address being a child of the Person class.

When building CustomerClient call address list that is in Person

When PersonLogin does not call the Address List that is in Person

When PersonFornecer call the address list that is in Person

.... so it goes.

Can anyone help me?

    
asked by anonymous 28.01.2015 / 18:12

1 answer

1

What I am presenting below is a modeling solution that does not use inheritance.

The changes I promoted in its original modeling were:

  • I removed the relationship from Address to Person, because an address does not need to know who lives there, is the person who has to say where it lives.

  • Customer is now called Client and does not inherit more from Person or anything.

  • Customer is related to Person. This relationship can not be changed because a client will never transform into another person, so the Person property is read only. The way to set the person there using NHibernate I do not know (I used the constructor).

  • User is related to Person. I assumed that this relationship also can not be changed and I used the same solution as the relationship between Client and Person.

It looks like this:

public class Pessoa
{  
    public virtual int IdPessoa { get; set; }
    public virtual string Nome { get; set; }
    public virtual IList<Endereco> Endereco { get; set; }
}

public class Endereco
{
    public virtual string Numero { get; set; }
    public virtual string Complemento { get; set; }
    public virtual string CEP { get; set; }
    public virtual string Cidade { get; set; }
    public virtual string UF { get; set; }
    public virtual string Pais { get; set; }
}

public class Cliente
{  
    public Cliente(Pessoa pessoa)
    {
        this.Pessoa = pessoa;
    }
    public virtual Pessoa Pessoa { get;}
    public virtual string Cnpj { get; set; }
    public virtual string InscrEstadual { get; set; }
    public virtual string Telefone { get; set; }
}

public class Usuário
{
    public Usuario(Pessoa pessoa)
    {
        this.Pessoa = pessoa;
    }
    public virtual string NomeUsuario { get; set; }
    public virtual string Senha { get; set; }
    public virtual Pessoa Pessoa { get;}
}

I have never used NHibernate so I will not risk mapping entities - I will restrict myself to modeling the entities already presented.

    
28.01.2015 / 20:16