Do not speak English ... then this is a translation can be confusing ... then ...
Galera to me with a problem here ... if someone already spent so perhaps I can help:
I have a table named
User
IdUsuario
Name
...
Another table named
Profile
IdPerfil
Name
...
And a third table that makes a relationship n / n with the two but with a field the most
UsuarioPerfil
IdUsuario
IdPerfil
Active
I am not considering implementing a class of user nor the hbm.xml it so that when I save the user can I add a usuarioperfil with the field set by active as true or false
Does anyone have any similar structure to help me make a
Usuario.cs:
Code:
using System;
using System.Collections.Generic;
using NHOR.DAL;
namespace NHOR
{
[Serializable]
public class Usuario: IEquatable<Usuario>
{
#region Private Members
private int _idusuario;
private string _nome;
private int? _idade;
private Uf _iduf;
private Cidade _idcidade;
#endregion
#region Constructor
public Usuario()
{
_idusuario = 0;
_nome = String.Empty;
_idade = new int?();
}
public Usuario(bool altera)
{
_idusuario = 0;
_nome = String.Empty;
_idade = new int?();
_iduf = new Uf();
_idcidade = new Cidade();
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Required Fields Only Constructor
/// <summary>
/// required (not null) fields only constructor
/// </summary>
public Usuario(
int idusuario,
string nome,Uf idUf, Cidade idCidade)
: this()
{
_idusuario = idusuario;
_nome = nome;
_idade = null;
_iduf = idUf;
_idcidade = idCidade;
}
#endregion // End Constructor
#region Public Properties
public virtual int IdUsuario
{
get
{
return _idusuario;
}
set
{
_idusuario = value;
}
}
public virtual string Nome
{
get
{
return _nome;
}
set
{
if( value == null )
throw new ArgumentOutOfRangeException("Null value not allowed for Nome", value, "null");
if( value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for Nome", value, value.ToString());
_nome = value;
}
}
public virtual int? Idade
{
get
{
return _idade;
}
set
{
_idade = value;
}
}
public virtual Uf IdUf
{
get
{
return _iduf;
}
set
{
if( value == null )
throw new ArgumentOutOfRangeException("Null value not allowed for IdUf", value, "null");
_iduf = value;
}
}
public virtual Cidade IdCidade
{
get
{
return _idcidade;
}
set
{
if( value == null )
throw new ArgumentOutOfRangeException("Null value not allowed for IdCidade", value, "null");
_idcidade = value;
}
}
#endregion
#region Public Functions
/// <summary>
/// Método para salvar o objeto
/// </summary>
/// <returns></returns>
public virtual Usuario SalvaUsuario(Usuario _objUsuario)
{
return USUARIODAL.SalvaUsuario(_objUsuario);
}
/// <summary>
/// Método para salvar o objeto
/// </summary>
/// <returns></returns>
public virtual Usuario SalvaUsuario()
{
return USUARIODAL.SalvaUsuario(this);
}
/// <summary>
/// Método para deletar o objeto
/// </summary>
/// <returns></returns>
public virtual void ExcluiUsuario(Usuario _objUsuario)
{
USUARIODAL.ExcluirUsuario(_objUsuario);
}
/// <summary>
/// Método para retornar todos itens
/// </summary>
/// <returns></returns>
public virtual IList<Usuario> RetornaUsuario()
{
return USUARIODAL.RetornaUsuario();
}
/// <summary>
/// Método para retornar um item
/// </summary>
/// <returns></returns>
public virtual Usuario RetornaUsuario(int id)
{
return USUARIODAL.RetornaUsuario(id);
}
#endregion //Public Functions
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Usuario castObj = (Usuario)obj;
return ( castObj != null ) &&
( this._idusuario == castObj.IdUsuario );
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 ^ hash ^ _idusuario.GetHashCode();
return hash;
}
#endregion
#region IEquatable members
public bool Equals(Usuario other)
{
if (other == this)
return true;
return ( other != null ) &&
( this._idusuario == other.IdUsuario );
}
#endregion
}
}
///
Usuario.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHOR" namespace="NHOR">
<class name="Usuario" table="USUARIO">
<id name="IdUsuario" column="idUsuario" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="nome" type="String" name="Nome" not-null="true" length="50" />
<property column="idade" type="Int32" name="Idade" />
<many-to-one name="IdUf" column="idUf" class="Uf" />
<many-to-one name="IdCidade" column="idCidade" />
</class>
</hibernate-mapping>
///
UsuarioPerfil.cs
Code:
using System;
using System.Collections.Generic;
namespace NHOR
{
[Serializable]
public class Usuarioperfil : IEquatable<Usuarioperfil>
{
#region Private Members
private int _idusuario;
private int _idperfil;
private bool? _ativo;
#endregion
#region Constructor
public Usuarioperfil()
{
_idusuario = 0;
_idperfil = 0;
_ativo = new bool?();
}
#endregion // End of Default ( Empty ) Class Constuctor
#region Required Fields Only Constructor
/// <summary>
/// required (not null) fields only constructor
/// </summary>
public Usuarioperfil(
int idusuario,
int idperfil)
: this()
{
_idusuario = idusuario;
_idperfil = idperfil;
_ativo = null;
}
#endregion // End Constructor
#region Public Properties
public virtual int IdUsuario
{
get
{
return _idusuario;
}
set
{
_idusuario = value;
}
}
public virtual int IdPerfil
{
get
{
return _idperfil;
}
set
{
_idperfil = value;
}
}
public virtual bool? Ativo
{
get
{
return _ativo;
}
set
{
_ativo = value;
}
}
#endregion
#region Public Functions
#endregion //Public Functions
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals(object obj)
{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != this.GetType())) return false;
Usuarioperfil castObj = (Usuarioperfil)obj;
return (castObj != null) &&
(this._idusuario == castObj.IdUsuario) &&
(this._idperfil == castObj.IdPerfil);
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 ^ hash ^ _idusuario.GetHashCode();
hash = 27 ^ hash ^ _idperfil.GetHashCode();
return hash;
}
#endregion
#region IEquatable members
public bool Equals(Usuarioperfil other)
{
if (other == this)
return true;
return (other != null) &&
(this._idusuario == other.IdUsuario) &&
(this._idperfil == other.IdPerfil);
}
#endregion
}
}
///
UsuarioPerfil.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHOR.Usuarioperfil,NHOR" table="usuarioperfil">
<composite-id unsaved-value="none">
<key-property name="IdUsuario" type="Int32" column="idUsuario"/>
<key-property name="IdPerfil" type="Int32" column="idPerfil"/>
</composite-id>
<property column="ativo" type="Boolean" name="Ativo" />
</class>
</hibernate-mapping>