Inheritance with Relationship of 0:N using Nhibernate

Asked

Viewed 505 times

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

    }
}

Personal

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


    }

 }

Personal login

   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");


    }


}

This way both Personal and Personal Login gets Address. How to

that Personcustomer has Address and Personal Informationlogin?

Could take Address from Parent Person Class, and make the relationship between Address and Personcustomer only that I will fall into another problem of column duplicity because this Address Class, will also be using in Personcontact,Personprovide,Personprofile that inherits from Person.

I wanted to leave Address being a daughter of the Pessoa class.

when building Personcustomer call address list that is in Person

when it is Personal

when it is Personal to call the address list that is in Person

.... so will.

Can someone help me?

  • Yes, I think I can help you: Let go of these heritages, they will give you more work than solution. For example, instead of Personal login, create a class User and relate it to Person, no inheritance. Personal and Personal, but probably worth the same approach: distinct classes and relationships instead of inheritance.

  • Hello Caffé has how you explain to me how it would be?

1 answer

1

What I present below is a non-heritage modeling solution.

The changes I promoted in their original modeling were:

  • Address ceases to inherit from person and becomes inheriting nothing.

  • 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 she lives.

  • Personal Customer becomes Customer and no longer inherits from Person or anything.

  • Client is related to Pessoa. This relationship cannot be changed because a client will never become another person, so the Person property is read-only. The way to set the person there using Nhibernate I don’t know (I used the builder).

  • Personal login becomes called User and also stops inheriting person and does not inherit from anyone.

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

Stayed 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 won’t risk for the mapping of entities - I will restrict myself to the modeling of entities already presented.

Browser other questions tagged

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