1
I am doing an application with ASP.Net MVC and Nhibernate, I have a company registration and a customer registration, where a company can have several clients. The classes are already mapped to the bank along with their relationship. Only the foreign client key that should catch the Id
primary key of this company null
, I’m not able to implement a logic so that when I record my client he can get the company’s primary key. My DAO is 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 mine controller this way:
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");
}
}
}
Loan:
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);
// }
}
}
Entidade Cliente:
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; }
}
}
Entity Empresa:
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; }
}
}
Mapping of the Company entity:
<?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>
View of Customer Registration:
@{
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>
I needed you to comment on what happens, because, I did not understand your doubt !!! It makes mistakes, it happens besides not bringing the code something else !!! ?
– Cezar