-->
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.  [ 5 posts ] 
Author Message
 Post subject: NHibernate 1.2.0 Beta 2 Error.. When I Delete Entity
PostPosted: Sat Nov 25, 2006 8:05 am 
Newbie

Joined: Thu Jan 19, 2006 7:24 am
Posts: 17
NHibernate 1.2.0 Beta 2 Error.. When I Delete Entity

When I try
Code:
UserAccountDAO dao = new UserAccountDAO( );
dao.DeleteEntity( 1 );


I Get Error Message
There is a problem with your mappings. You are probably trying to map a System.ValueType to a <class> which NHibernate does not allow or you are incorrectly using the IDictionary that is mapped to a <set>.

A ValueType (System.Int64) can not be used with IdentityKey. The thread at google has a good description about what happens with boxing and unboxing ValueTypes and why they can not be used as an IdentityKey: http://groups.google.com/groups?hl=en&l ... 26tab%3Dwg
Parameter name: key
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: There is a problem with your mappings. You are probably trying to map a System.ValueType to a <class> which NHibernate does not allow or you are incorrectly using the IDictionary that is mapped to a <set>.

A ValueType (System.Int64) can not be used with IdentityKey. The thread at google has a good description about what happens with boxing and unboxing ValueTypes and why they can not be used as an IdentityKey: http://groups.google.com/groups?hl=en&l ... 26tab%3Dwg
Parameter name: key



Entity Class
Code:
using System;
using System.Collections.Generic;
//using NHibernate.Collection.Generic;


namespace RealEstate.BusinessEntity
{
   [Serializable]
   public class UserAccount: IEquatable<UserAccount>
   {

      #region Private Members

      private long? _user_id;
      private IList<ProductItem> _ProductItemList;
      private IList<TempCustomerDetails> _TempCustomerDetailsList;
      private IList<UserInRole> _UserInRoleList;
      private IList<CustomerDetails> _CustomerDetailsList;
      private string _username;
      private string _email;
      private string _password;
      private DateTime _create_date;
      private DateTime _last_login_date;
      private string _reset_password_code;       
      #endregion

      #region Constructor

      public UserAccount()
      {
         this._user_id = 0;
         this._ProductItemList = new List<ProductItem>();
         this._TempCustomerDetailsList = new List<TempCustomerDetails>();
         this._UserInRoleList = new List<UserInRole>();
         this._CustomerDetailsList = new List<CustomerDetails>();
         this._username = String.Empty;
         this._email = String.Empty;
         this._password = String.Empty;
         this._create_date = DateTime.MinValue;
         this._last_login_date = DateTime.MinValue;
         this._reset_password_code = String.Empty;
      }
      #endregion // End of Default ( Empty ) Class Constuctor

      #region Public Properties
         
      public virtual long? UserId
      {
         get { return this._user_id; }
         set { this._user_id = value; }
      }
         
      public virtual IList<ProductItem> ProductItemList

      {
         get { return this._ProductItemList; }
         set { this._ProductItemList = value; }
      }

      public virtual IList<TempCustomerDetails> TempCustomerDetailsList

      {
         get { return this._TempCustomerDetailsList; }
         set { this._TempCustomerDetailsList = value; }
      }

      public virtual IList<UserInRole> UserInRoleList

      {
         get { return this._UserInRoleList; }
         set { this._UserInRoleList = value; }
      }

      public virtual IList<CustomerDetails> CustomerDetailsList

      {
         get { return this._CustomerDetailsList; }
         set { this._CustomerDetailsList = value; }
      }

      public virtual string Username
      {
         get { return this._username; }

         set   
         {   
            if( value == null )
               throw new ArgumentOutOfRangeException("Null value not allowed for Username", value, "null");
            
            if(  value.Length > 100)
               throw new ArgumentOutOfRangeException("Invalid value for Username", value, value.ToString());
            
            _username = value;
         }
      }
         
      public virtual string Email
      {
         get { return this._email; }

         set   
         {   
            if(  value != null &&  value.Length > 50)
               throw new ArgumentOutOfRangeException("Invalid value for Email", value, value.ToString());
            
            _email = value;
         }
      }
         
      public virtual string Password
      {
         get { return this._password; }

         set   
         {   
            if( value == null )
               throw new ArgumentOutOfRangeException("Null value not allowed for Password", value, "null");
            
            if(  value.Length > 200)
               throw new ArgumentOutOfRangeException("Invalid value for Password", value, value.ToString());
            
            _password = value;
         }
      }
         
      public virtual DateTime CreateDate
      {
         get { return this._create_date; }
         set { this._create_date = value; }
      }
         
      public virtual DateTime LastLoginDate
      {
         get { return this._last_login_date; }
         set { this._last_login_date = value; }
      }
         
      public virtual string ResetPasswordCode
      {
         get { return this._reset_password_code; }

         set   
         {   
            if(  value != null &&  value.Length > 100)
               throw new ArgumentOutOfRangeException("Invalid value for ResetPasswordCode", value, value.ToString());
            
            _reset_password_code = value;
         }
      }
         
            
      #endregion

      #region Public Functions

      public virtual void AddProductItem(ProductItem obj)
      {
         if (obj == null)
            throw new ArgumentNullException("obj", "Null value not allowed.");
            
         this._ProductItemList.Add(obj);
      }
      

      public virtual void AddTempCustomerDetails(TempCustomerDetails obj)
      {
         if (obj == null)
            throw new ArgumentNullException("obj", "Null value not allowed.");
            
         this._TempCustomerDetailsList.Add(obj);
      }
      

      public virtual void AddUserInRole(UserInRole obj)
      {
         if (obj == null)
            throw new ArgumentNullException("obj", "Null value not allowed.");
            
         this._UserInRoleList.Add(obj);
      }
      

      public virtual void AddCustomerDetails(CustomerDetails obj)
      {
         if (obj == null)
            throw new ArgumentNullException("obj", "Null value not allowed.");
            
         this._CustomerDetailsList.Add(obj);
      }
      

      #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;
         UserAccount castObj = (UserAccount)obj;
         return ( castObj != null ) &&
            ( this._user_id == castObj.UserId );
      }
      
      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         
         int hash = 57;
         hash = 27 ^ hash ^ _user_id.GetHashCode();
         return hash;
      }
      #endregion
      
      #region IEquatable members

      public bool Equals(UserAccount other)
      {
         if (other == this)
            return true;
      
         return ( other != null ) &&
            ( this._user_id == other.UserId );
               
      }

      #endregion
      
   }
}


[b]Mapping[/b]
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="RealEstate.BusinessEntity" namespace="RealEstate.BusinessEntity">
   <class name="UserAccount" table="user_account">

      <id name="UserId" column="USER_ID" type="Int64" unsaved-value="0">
         <generator class="native"/>
      </id>
      <bag name="ProductItemList" inverse="true" lazy="true" >
         <key column="USER_POST_BY" />
         <one-to-many class="ProductItem" />
      </bag>
      <bag name="TempCustomerDetailsList" inverse="true" lazy="true" >
         <key column="USER_ID" />
         <one-to-many class="TempCustomerDetails" />
      </bag>
      <bag name="UserInRoleList" inverse="true" lazy="true" >
         <key column="USER_ID" />
         <one-to-many class="UserInRole" />
      </bag>
      <bag name="CustomerDetailsList" inverse="true" lazy="true" >
         <key column="USER_ID" />
         <one-to-many class="CustomerDetails" />
      </bag>
      <property column="USERNAME" type="String" name="Username" not-null="true" length="100" />
      <property column="EMAIL" type="String" name="Email" length="50" />
      <property column="PASSWORD" type="String" name="Password" not-null="true" length="200" />
      <property column="CREATE_DATE" type="DateTime" name="CreateDate" not-null="true" />
      <property column="LAST_LOGIN_DATE" type="DateTime" name="LastLoginDate" />
      <property column="RESET_PASSWORD_CODE" type="String" name="ResetPasswordCode" length="100" />
      
   </class>
</hibernate-mapping>



[b]DAO Class[/b]
Code:
   public abstract class NHibernateDataAccess<T, K> : IDomainDAO<T, K>
   {      
      public void DeleteEntity( K id )
      {
         ITransaction trans = this.BeginTransaction();
         
         try
         {
            NHibernateUtils<T>.Delete( id );
            trans.Commit();
         }   
         catch
         {
            trans.Rollback();
            throw;
         }
      }

   }

   public class UserAccountDAO : NHibernateDataAccess<UserAccount, long?>
   {
      public UserAccountDAO()
      {
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 25, 2006 2:08 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You posted a lot of code, but missed the important piece - what does NHibernateUtils<T>.Delete() look like?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 25, 2006 5:11 pm 
Newbie

Joined: Thu Jan 19, 2006 7:24 am
Posts: 17
Delete Source Code
Code:
public class NHibernateUtils<T>
{
      public static void Delete( Object id )
      {
         try
         {
            NHibernateWebSessionManagement.Session.Delete( id );
         }
         catch( Exception )
         {
            throw;
         }
      }
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 25, 2006 6:18 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Session.Delete() accepts either a string, which is the HQL to find the items to delete, or the object (entity) to delete.
It does not accept the object id.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 2:22 am 
Newbie

Joined: Thu Jan 19, 2006 7:24 am
Posts: 17
Thank u

I 'll re-write my source code now.


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

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.