Fill foreign key with primary key value

1

I'm doing an application with ASP.Net MVC and NHibernate, I have a business registration and a customer registration, where a company can have multiple clients. The classes are already mapped to the bank along with your relationship. Only the foreign client key that should catch the Id of the primary key of the company is null , I am not able to implement a logic so that when I write my client it can get the primary key of the company. My DAO looks like this:

using SistemaOCW.Entidade;
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using SistemaOCW.Infra;
using NHibernate;

namespace SistemaOCW.DAO
{
    public class ClienteDAO
    {
        private ISession session;


        public ClienteDAO(ISession session)
        {
            this.session = session;
        }    

        public void Adiciona(Cliente cliente)
        {
            NHibernate.ITransaction transacao = session.BeginTransaction();
            session.Save(cliente);
            transacao.Commit();
        }    

        // public Cliente BuscaPorId(int id)
        //  {
        //  return session.Get<Cliente>(id);
        //  }
    }
}

And my controller looks like this:

using NHibernate;
using SistemaOCW.DAO;
using SistemaOCW.Entidade;
using SistemaOCW.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;    

namespace SistemaOCW.Controllers
{
    public class ClienteController : Controller
    {    
        public ActionResult Form()
        {
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Adiciona(Cliente cliente)
        {    
            ISession session = NHibernateHelper.AbreSession();

            ClienteDAO dao = new ClienteDAO(session);

            dao.Adiciona(cliente);
            session.Close();
            return RedirectToAction("Index");
        }        
    }
}

Company:

using SistemaOCW.Entidade;
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using SistemaOCW.Infra;
using NHibernate;
using SistemaOCW.Controllers;

namespace SistemaOCW.DAO
{
    public class EmpresaDAO
    {
        private ISession session;

        public EmpresaDAO(ISession session)
        {
            this.session = session;
        }

        public void Adiciona(Empresa empresa)
        {
            NHibernate.ITransaction transacao = session.BeginTransaction();
            session.Save(empresa);
            transacao.Commit();
        }

        // public Cliente BuscaPorId(int id)
        //  {
        //      return session.Get<Cliente>(id);
        //  }
    }
}

Client Entity:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SistemaOCW.Entidade
{
    public class Cliente
    {
        public virtual int Id { get; set; }
        public virtual int Codcliente { get; set; }
        public virtual string Nome { get; set; }
        public virtual string Cnpj { get; set; }
        public virtual string Endereco { get; set; }
        public virtual string Bairro { get; set; }
        public virtual string Cidade { get; set; }
        public virtual string Cep { get; set; }
        public virtual string Telefone { get; set; }
        public virtual string Email { get; set; }
        public virtual string Contato { get; set; }
        public virtual Empresa empresa { get; set; }
    }
}

Company Entity:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SistemaOCW.Entidade
{
    public class Empresa
    {
        public virtual int Id { get; set; }
        public virtual string Nome { get; set; }
        public virtual string Cnpj { get; set; }
        public virtual string Endereco { get; set; }
        public virtual string Bairro { get; set; }
        public virtual string Cidade { get; set; }
        public virtual string Cep { get; set; }
        public virtual string Telefone { get; set; }
        public virtual string Email { get; set; }
        public virtual string Contato { get; set; }
        public virtual IList<Cliente> Clientes { get; set; }
    }
}

Company Entity Mapping:

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="SistemaOCW"
                       namespace="SistemaOCW.Entidade">
      <class name ="Empresa">
        <id name ="Id">
            <generator class ="identity"/>
        </id>
        <property name="Nome"/>
        <property name="Cnpj"/>
        <property name="Endereco"/>
        <property name="Bairro"/>
        <property name="Cidade"/>
        <property name="Cep"/>
        <property name="Telefone"/>
        <property name="Email"/>
        <property name="Contato"/> 
        <bag name="Clientes">
          <key column= "EmpresaId"/>
          <one-to-many class="Cliente"/>  
        </bag>
      </class>
    </hibernate-mapping>

Customer registration view:

@{
    ViewBag.Title = "Form";
}

<h2>Cadastro Cliente</h2>

<form action="@Url.Action("Adiciona", "Cliente")" method="post">
    <label>
        Codigo:
        <input type="text" name="cliente.Codcliente" />
        Nome:
        <input type="text" name="cliente.Nome" />
         CNPJ:
        <input type="text" name="cliente.Cnpj" />
         Endereco:
        <input type="text" name="cliente.Endereco" />
         Bairro:
        <input type="text" name="cliente.Bairro" />
         Cidade:
        <input type="text" name="cliente.Cidade" />
         CEP:
        <input type="text" name="cliente.Cep" />
         Telefone:
        <input type="text" name="cliente.Telefone" />
         Email:
        <input type="text" name="cliente.Email" />
         Contato:
        <input type="text" name="cliente.Contato" />
    </label>
    <input type="submit" value="Adicionar"/>
</form>
    
asked by anonymous 26.11.2014 / 20:20

1 answer

1

In the mapping you can set up for NHibernate to automatically generate the ID for you, so you will have something like this if you are using Nhibernate :

<id name="Id">
    <generator class="identity"/>
</id>

In the mapping you will get something like this if you are using Fluent Nhibernate :

Id(x => x.Id).GeneratedBy.Increment();

Verify that you are in the correct configuration, or if you have configured to generate the ID in a different way (other than automatic).

If this setting is correct and it still does not solve your problem, please place the mapping files so we can try to help.

    
26.11.2014 / 21:39