These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Unexpected row count: 0; expected: 1
PostPosted: Mon Nov 19, 2007 10:01 am 
Newbie

Joined: Wed Nov 14, 2007 9:44 am
Posts: 4
When I try to save a n / n using the set, and that the class contains attributes most of this error.

Someone would say this because I

Diagram

Image

classe Usuario.hbm.xml

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHOR" namespace="NHOR" default-cascade="save-update">
   <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="Cidade" />
    <set name="UsuarioperfilList">
      <key column="idUsuario"/>
      <many-to-many class="Usuarioperfil">
        <column name="IdUsuario"/>
        <column name="IdPerfil"/>
      </many-to-many>
    </set>
  </class>
</hibernate-mapping>


class Usuario.cs

Code:
using System;
using System.Collections.Generic;
using System.Collections;
using NHOR.DAL;
using Iesi.Collections;


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;
        private ISet _UsuarioperfilList;
        #endregion

        #region Constructor

        public Usuario()
        {
            _idusuario = 0;
            _nome = String.Empty;
            _idade = new int?();
            _UsuarioperfilList = new HashedSet();
        }
        public Usuario(bool altera)
        {
            _idusuario = 0;
            _nome = String.Empty;
            _idade = new int?();
            _iduf = new Uf();
            _idcidade = new Cidade();
            _UsuarioperfilList = new HashedSet();
        }
        #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;
            }

        }
        public virtual ISet UsuarioperfilList
        {
            get
            {
                return _UsuarioperfilList;
            }
            set
            {
                _UsuarioperfilList = value;
            }
        }

        #endregion

        #region Public Functions


        public virtual void AddUsuarioperfil(Usuarioperfil obj)
        {
            #region Check if null
            if (obj == null)
                throw new ArgumentNullException("obj", "Il parametro non può essere nullo");
            #endregion
            _UsuarioperfilList.Add(obj);
        }

        /// <summary>
        /// Método para salvar o objeto
        /// </summary>
        /// <returns></returns>
        public virtual Usuario SalvaUsuario(Usuario _objUsuario)
        {
            return USUARIODAL.SalvaUsuario(_objUsuario);
        }
        /// 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

    }
}



Classe Usuarioperfil.hbm.xml

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHOR" namespace="NHOR" default-cascade="save-update">
  <class name="Usuarioperfil" 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>


class 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;
        }
        public Usuarioperfil(
            int idusuario,
            int idperfil,bool ativo)
            : this()
        {
            _idusuario = idusuario;
            _idperfil = idperfil;
            _ativo = ativo;
        }
        #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

    }
}


Salvando


Code:
public static Usuario SalvaUsuario(Usuario _objUsuario)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();
            try
            {
                session.Transaction.Begin();
                session.SaveOrUpdate(_objUsuario);
                session.Transaction.Commit();
                session.Flush();
                session.Close();
            }
            catch (Exception ex)
            {
                session.Transaction.Rollback();
                throw ex;
            }
            return _objUsuario;
        }
       


Code:
NHibernateHelper.CloseSession();
        Usuario u = new Usuario(true);
        u.Idade = Convert.ToInt32(IdadeTextBox.Text);
        u.IdCidade.IdCidade = Convert.ToInt32(IdCidadeDropDownList.Text);
        u.IdUf.IdUf = Convert.ToInt32(IdUfDropDownList.SelectedValue);
        u.Nome = NomeTextBox.Text;
        if (IdUsuarioLabel.Text != "")
        {
            u.IdUsuario = Convert.ToInt32(IdUsuarioLabel.Text);
        }
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected == true)
            {
                Usuarioperfil us = new Usuarioperfil();
                us.Ativo = true;
                us.IdPerfil = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                u.UsuarioperfilList.Add(us);
            }
        }
       
        u = u.SalvaUsuario();

_________________
get{ return heliegesio;}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.