Fluent NHibernate with multiple tables

3

I'm using Fluent NHibernate to help with ORM (replacing .hbm files manually) with a single class:

User Class:

public class Usuario
{
    public virtual int Id { get; set; }
    public virtual string Nome { get; set; }
    public virtual string Login { get; set; }
    public virtual string Senha { get; set; }
    public virtual char Status { get; set; }

}

Class Mapping:

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        Table("usuarios");
    }
}

Adding mapping to Fluent NHibernate

var configMap = Fluently.Configure().Database(configDB).Mappings(c => c.FluentMappings.AddFromAssemblyOf<Mapeamento.UsuarioMap>());
  

Question: Now I need to add another class (Address, the User instance will have an Address property) and a Vendor class (which is not bound to any other)   what would this mapping look like?

New User Class:

public class Usuario
{
    public virtual int Id { get; set; }
    public virtual string Nome { get; set; }
    public virtual string Login { get; set; }
    public virtual string Senha { get; set; }
    public virtual char Status { get; set; }
    public virtual Endereco EnderecoResidencial { get; set; }
}

Address Class:

public class Endereco
{
    public virtual string Rua { get; set; }
    public virtual string Quadra { get; set; }
    public virtual string Lote { get; set; }
    public virtual string Bairro { get; set; }
    public virtual string CEP { get; set; }
    public virtual string Complemento { get; set; }
    public virtual string PontoReferencia { get; set; }
}

Supplier Class:

public class Fornecedor
{
    public virtual string CNPJ { get; set; }
    public virtual string NomeFantasia { get; set; }
    public virtual string RazaoSocial { get; set; }
    public virtual string Descricao { get; set; }
}
    
asked by anonymous 23.12.2015 / 14:54

1 answer

3
Assuming your usuario table has an FK for the endereco table, you would have something like this:

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        References(c => c.EnderecoResidencial).Column("endereco_id");
        Table("usuarios");
    }
}

Your class endereco must have an ID:

public class Endereco
{
    public virtual int Id { get; set; }
    public virtual char Rua { get; set; }
    public virtual char Quadra { get; set; }
    public virtual char Lote { get; set; }
    public virtual char Bairro { get; set; }
    public virtual char CEP { get; set; }
    public virtual char Complemento { get; set; }
    public virtual char PontoReferencia { get; set; }
}

And its mapping of endereco :

public class EnderecoMap : ClassMap<Endereco>
{
    public EnderecoMap()
    {
        Id(c => c.Id);
        Map(c => c.Rua);
        Map(c => c.Quadra);
        Map(c => c.Lote);
        Map(c => c.Bairro);
        Map(c => c.CEP);
        Map(c => c.Complemento);
        Map(c => c.PontoReferencia); 
        Table("enderecos");
    }
}

I recommend using LazyLoad() in the mapping of the usuario entity, in order to avoid that every time you make a select of one or more usuario , come the whole entity Address. If you want, I'll edit the answer with an example of using LazyLoad() .

EDIT:

As Fornecedor has no relation to any class, it would just be "one more" in the assembly of Mapeamento.UsuarioMap :

public class FornecedorMap : ClassMap<Fornecedor>
{
    public FornecedorMap()
    {
        Map(c => c.CNPJ);
        Map(c => c.NomeFantasia);
        Map(c => c.RazaoSocial);
        Map(c => c.Descricao);
        Table("fornecedor");
    }
}

EDIT 2:

EXAMPLE OF USE WITH LazyLoad() : In all mappings you want to use LazyLoad() , just add it as shown below.

public class UsuarioMap : ClassMap<Usuario>
{
    public UsuarioMap()
    {
        Table("usuarios");
        LazyLoad();
        Id(c => c.Id);
        Map(c => c.Nome);
        Map(c => c.Login);
        Map(c => c.Senha);
        Map(c => c.Status);
        References(c => c.EnderecoResidencial).Column("endereco_id");
    }
}

In this case, when you query a list of users, for example:

using (var session = NHibernateSession.OpenSession())
{    
    var listaUsuarios = session.Query<Usuario>();
}

You will have the propriedades of user entered in the query and ONLY the foreign key of the Endereco entity. If you NEED the entity TODA, just give a Fetch to the entity:

using (var session = NHibernateSession.OpenSession())
{    
     var listaUsuarios = session.Query<Usuario>().Fetch(x=>x.EnderecoResidencial);
}

Then beyond the properties, you will also have the entire entity you are fetching.

    
23.12.2015 / 15:07