-->
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: Help map/dictionary mapping
PostPosted: Sun Jul 20, 2008 11:51 am 
Newbie

Joined: Wed Jul 09, 2008 7:48 pm
Posts: 5
Having problems mapping the below mapping files/classes

Below is the mapping for my map/dictionary

Code:
   <class name="ViewContainerInstance" table="vici_view_container_instance" lazy="false">

      <!-- Primary Key(s) -->
      <id name="Id" column="vici_pk" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
      
      <!-- Properties -->
      <property column="vici_short_description" type="String" name="ShortDescription" />
      <property column="vici_description" type="String" name="Description" />
      <property column="vici_order" type="Int32" name="Order" />
      
      <!-- One-To-Many relations -->
    <map name="Properties" cascade="all" >
      <key column="vcip_fk_vici"/>
      <index column="vcip_name" type="System.String"/>
      <one-to-many class="ViewContainerInstanceProperty"/>
    </map>
   </class>

Code:
namespace View
{
   /// <summary>
   /// </summary>
   [Serializable]
   public class ViewContainerInstance
   {
      #region Private Members
      
      // Variabili di stato
      private bool m_isChanged;
      private bool m_isDeleted;

      // Primary Key(s)
      private int m_vici_pk;
      
      // Properties
      private string m_vici_short_description;
      private string m_vici_description;
      private int? m_vici_order;
      
      // One-to-many relations
        private IDictionary<string, ViewContainerInstanceProperty> m_listViewContainerInstanceProperty;

        private string m_TemporaryId;

      #endregion
      
      #region Default ( Empty ) Class Constructor
      
      /// <summary>
      /// default constructor
      /// </summary>
      public ViewContainerInstance()
      {
            m_TemporaryId = Guid.NewGuid().ToString().Replace("-", "");
         m_vici_pk = 0;
         m_vici_short_description = null;
         m_vici_description = null; 
         m_vici_order = null;
         m_listViewContainerInstanceProperty = new Dictionary<string, ViewContainerInstanceProperty>();
      }
      
      #endregion // End of Default ( Empty ) Class Constructor

      #region Public Properties
         
      /// <summary>
      ///
      /// </summary>      
      public virtual int Id
      {
         get { return m_vici_pk; }
         set { m_isChanged |= (m_vici_pk != value); m_vici_pk = value; }
      }
         
      /// <summary>
      ///
      /// </summary>      
      public virtual string ShortDescription
      {
         get { return m_vici_short_description; }
         set   
         {
            m_isChanged |= (m_vici_short_description != value); m_vici_short_description = value;
         }
      }
    
      /// <summary>
      ///
      /// </summary>      
      public virtual string Description
      {
         get { return m_vici_description; }
         set   
         {
            m_isChanged |= (m_vici_description != value); m_vici_description = value;
         }
      }
    
      /// <summary>
      ///
      /// </summary>      
      public virtual int? Order
      {
         get { return m_vici_order; }
         set { m_isChanged |= (m_vici_order != value); m_vici_order = value; }
      }
    
      /// <summary>
      ///
      /// </summary>      
      public virtual IDictionary<string, ViewContainerInstanceProperty> Properties
      {
         get { return m_listViewContainerInstanceProperty; }
         set { m_isChanged |= (m_listViewContainerInstanceProperty != value); m_listViewContainerInstanceProperty = value; }
      }
    
      /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public virtual bool IsChanged
      {
         get { return m_isChanged; }
      }
      
      /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public virtual bool IsDeleted
      {
         get { return m_isDeleted; }
      }
      
      #endregion
      
      #region Public Functions

      /// <summary>
      /// mark the item as deleted
      /// </summary>
      public virtual void MarkAsDeleted()
      {
         m_isDeleted = true;
         m_isChanged = true;
      }
      
      #endregion
      
      #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;
         ViewContainerInstance castObj = (ViewContainerInstance)obj;
         return ( castObj != null ) &&
            ( this.m_vici_pk == castObj.Id );
      }
      
      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         int hash = 57;
         hash = 27 * hash * this.m_vici_pk.GetHashCode();
               
         return hash;      
         
      }
      
      #endregion
      
   }
}

a here is the mapping for the elements in the collection.

Code:
<class name="ViewContainerInstanceProperty" table="vcip_view_container_instance_property" lazy="false">
    <composite-id>
      <key-many-to-one name="Parent" class="ViewContainerInstance">
        <column name="vcip_fk_vici" sql-type="int"/>
      </key-many-to-one>
      <key-property column="vcip_name" type="String" name="Name"  />
    </composite-id>
    <property column="vcip_value" type="String" name="Value"/>
  </class>

Code:
namespace View
{
   /// <summary>
   /// </summary>
   [Serializable]
   public class ViewContainerInstanceProperty
   {
      #region Private Members
      
      // Variabili di stato
      private bool m_isChanged;
      private bool m_isDeleted;

      // Primary Key(s)
      private ViewContainerInstance m_vcip_fk_vici;
        private int m_ParentId;
      private string m_vcip_name;
      
      // Properties
      private object m_vcip_value;
        private bool m_CanSave;
        private bool m_CanTransfer;

      #endregion
      
      #region Default ( Empty ) Class Constructor
      
      /// <summary>
      /// default constructor
      /// </summary>
      public ViewContainerInstanceProperty()
      {
            m_Id = 0;
            m_ParentId = 0;
         m_vcip_fk_vici = null;
         m_vcip_name = null;
         m_vcip_value = null;
            m_CanSave = false;
            m_CanTransfer = false;
      }
      
      #endregion // End of Default ( Empty ) Class Constructor

        public ViewContainerInstanceProperty(string name, bool canSave, object value)
        {
            m_CanSave = canSave;
            m_vcip_name = name;
            m_vcip_value = value;
        }

      #region Public Properties

        private int m_Id;

        public virtual int Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        public int ParentId
        {
            get { return m_ParentId; }
            set { m_ParentId = value; }
        }
   
      /// <summary>
      ///
      /// </summary>      
        public virtual ViewContainerInstance Parent
      {
         get { return m_vcip_fk_vici; }
         set { m_isChanged |= (m_vcip_fk_vici != value); m_vcip_fk_vici = value; }
      }
    
      /// <summary>
      ///
      /// </summary>      
      public virtual string Name
      {
         get { return m_vcip_name; }
         set   
         {
            m_isChanged |= (m_vcip_name != value); m_vcip_name = value;
         }
      }
    
      /// <summary>
      ///
      /// </summary>      
      public virtual object Value
      {
         get { return m_vcip_value; }
         set   
         {
            m_isChanged |= (m_vcip_value != value); m_vcip_value = value;
         }
      }

        /// <summary>
        ///
        /// </summary>      
        public virtual bool CanSave
        {
            get { return m_CanSave; }
            set
            {
                m_CanSave = value;
            }
        }

        /// <summary>
        ///
        /// </summary>      
        public virtual bool CanTransfer
        {
            get { return m_CanTransfer; }
            set
            {
                m_CanTransfer = value;
            }
        }
       
        /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public virtual bool IsChanged
      {
         get { return m_isChanged; }
      }
      
      /// <summary>
      /// Returns whether or not the object has changed it's values.
      /// </summary>
      public virtual bool IsDeleted
      {
         get { return m_isDeleted; }
      }
      
      #endregion
      
      #region Public Functions

      /// <summary>
      /// mark the item as deleted
      /// </summary>
      public virtual void MarkAsDeleted()
      {
         m_isDeleted = true;
         m_isChanged = true;
      }
      
      #endregion
      
      #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;
         ViewContainerInstanceProperty castObj = (ViewContainerInstanceProperty)obj;
         return ( castObj != null ) &&
            ( this.m_vcip_fk_vici == castObj.Parent ) &&
            ( this.m_vcip_name == castObj.Name );
      }
      
      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         int hash = 57;
         hash = 27 * hash * this.m_vcip_fk_vici.GetHashCode();
         hash = 27 * hash * this.m_vcip_name.GetHashCode();
               
         return hash;      
         
      }
      
      #endregion
      
   }
}


Now the above works for straight inserts, but when attempting an update I get an NonUniqueObjectException exception.

I assume it's because NHibernate can't determine if it's already been inserted e.g. there is no unsaved-value.

How do I map the above? please someone help


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.