-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping problem with composite id and parent/child relatio..
PostPosted: Mon Jul 28, 2008 10:32 am 
Newbie

Joined: Mon Jul 28, 2008 10:16 am
Posts: 4
Hi,

I'm currently mapping a legacy database and I have come across a tricky relationship that I can't seem to map correctly.

An SnmpManager can have multiple SnmpManagerExcludedOids associated with it. So I have a generic collection of SnmpManagerExcludedOids in my SnmpManager object. The SnmpManagerExcludedOids identity is a composite key of a string and the manager_id from SnmpManager.

When I add a new SnmpManagerExcludedOids to the generic collection in SnmpManager, the child relationship is not persisted.

Is there something simple I'm missing/doing wrong in my mappings?

Any help would be gratefully received.

Regards,

Paul

Hibernate version:
NHibernate 2.0.0.2002

Table Schema:
Code:

CREATE TABLE [snmp_managers] (
   [id] [int] NOT NULL ,
   [name] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
   [hostname] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
   [port] [int] NOT NULL CONSTRAINT [df_snmp_managers_port] DEFAULT (162),
   [community] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
   [trap_protocol] [int] NOT NULL CONSTRAINT [df_snmp_managers_trap_protocol] DEFAULT (0),
   [min_severity] [tinyint] NOT NULL CONSTRAINT [df_snmp_managers_min_severity] DEFAULT (0),
   [disabled] [bit] NOT NULL CONSTRAINT [df_snmp_managers_disabled] DEFAULT (0),
   [last_update] [datetime] NOT NULL CONSTRAINT [df_snmp_managers_last_update] DEFAULT (getdate()),
   CONSTRAINT [pk_snmp_managers] PRIMARY KEY  CLUSTERED
   (
      [id]
   ) WITH  FILLFACTOR = 90  ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [snmp_manager_excluded_oids] (
   [manager_id] [int] NOT NULL ,
   [oid] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
   CONSTRAINT [pk_snmp_manager_excluded_oids] PRIMARY KEY  CLUSTERED
   (
      [manager_id],
      [oid]
   ) WITH  FILLFACTOR = 90  ON [PRIMARY] ,
   CONSTRAINT [fk_snmp_manager_excluded_oids_snmp_managers] FOREIGN KEY
   (
      [manager_id]
   ) REFERENCES [snmp_managers] (
      [id]
   )
) ON [PRIMARY]
GO



Mapping documents:

Snmp_Managers.hbm.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Digitalk.Gateway.Core.DomainObjects.SnmpManagers,Digitalk.Gateway.Core" table="snmp_managers" lazy="true">
    <id name="ID" column="id" type="int">
      <generator class="assigned" />
    </id>
    <property name="Name" column="name" type="string" />
    <property name="Hostname" column="hostname" type="string" not-null="true" />
    <property name="Port" column="port" type="int" />
    <property name="Community" column="community" type="string" />
    <property name="TrapProtocol" column="trap_protocol" type="int" />
    <property name="MinSeverity" column="min_severity" type="byte" />
    <property name="Disabled" column="disabled" type="Boolean" />
    <property name="LastUpdate" column="last_update" type="DateTime" />
    <bag name="SnmpManagerExcludedOids" inverse="true" lazy="true" cascade="all-delete-orphan">
      <key column="manager_id" />
      <one-to-many class="Digitalk.Gateway.Core.DomainObjects.SnmpManagerExcludedOids,Digitalk.Gateway.Core" />
    </bag>
  </class>
</hibernate-mapping>


Snmp_Managers.hbm.xml.cs
Code:
/// <summary>
    /// SnmpManagers object for NHibernate mapped table 'snmp_managers'.
    /// </summary>
    [Serializable]
    [DataContract]
    public class SnmpManagers : DomainObject<int>
    {
        #region Member Variables

        protected string _community;
        protected bool _disabled;
        protected string _hostname;
        protected DateTime _lastupdate;
        protected byte _minseverity;
        protected string _name;
        protected int _port;
        protected IList<SnmpManagerExcludedOids> _snmpmanagerexcludedoids = new List<SnmpManagerExcludedOids>();
        protected int _trapprotocol;

        #endregion

        #region Constructors

        #endregion

        #region Public Properties
        [DataMember]
        public virtual string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value.Length > 20)
                    throw new ArgumentOutOfRangeException("value", value, "Name cannot contain more than 20 characters");
                _name = value;
            }
        }
       
        [DataMember]
        public virtual string Hostname
        {
            get { return _hostname; }
            set
            {
                if (value != null && value.Length > 64)
                    throw new ArgumentOutOfRangeException("value", value,
                                                          "Hostname cannot contain more than 64 characters");
                _hostname = value;
            }
        }
       
        [DataMember]
        public virtual int Port
        {
            get { return _port; }
            set { _port = value; }
        }
       
        [DataMember]
        public virtual string Community
        {
            get { return _community; }
            set
            {
                if (value != null && value.Length > 50)
                    throw new ArgumentOutOfRangeException("value", value,
                                                          "Community cannot contain more than 50 characters");
                _community = value;
            }
        }
       
        [DataMember]
        public virtual int TrapProtocol
        {
            get { return _trapprotocol; }
            set { _trapprotocol = value; }
        }
       
        [DataMember]
        public virtual byte MinSeverity
        {
            get { return _minseverity; }
            set { _minseverity = value; }
        }
       
        [DataMember]
        public virtual bool Disabled
        {
            get { return _disabled; }
            set { _disabled = value; }
        }
       
        [DataMember]
        public virtual DateTime LastUpdate
        {
            get { return _lastupdate; }
            set { _lastupdate = value; }
        }
       
        [DataMember]
        public virtual IList<SnmpManagerExcludedOids> SnmpManagerExcludedOids
        {
            get
            {
                return _snmpmanagerexcludedoids;
            }
            set
            {
                _snmpmanagerexcludedoids = value;
            }
        }
        #endregion

        #region Equals And HashCode Overrides

        /// <summary>
        /// local implementation of GetHashCode based on unique value members
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 57;
            hash = 27*hash*ID.GetHashCode();
            return hash;
        }

        #endregion
    }


SnmpManagerExcludedOids.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Digitalk.Gateway.Core.DomainObjects.SnmpManagerExcludedOids,Digitalk.Gateway.Core" table="snmp_manager_excluded_oids" lazy="true">
    <composite-id name="ID" class="Digitalk.Gateway.Core.DomainObjects.SnmpManagerExcludedOidsId,Digitalk.Gateway.Core">
      <key-property name="ManagerId" column="manager_id" type="int" />
      <key-property name="Oid" column="oid" type="string" />
    </composite-id>
  </class>
</hibernate-mapping>


Code:
/// <summary>
   /// SnmpManagerExcludedOids object for NHibernate mapped table 'snmp_manager_excluded_oids'.
   /// </summary>
   [Serializable]
    [DataContract]
   public class SnmpManagerExcludedOids : DomainObject<SnmpManagerExcludedOidsId>
    {
      #region Constructors
         
      public SnmpManagerExcludedOids() {}
            
      #endregion
      
      #region Equals And HashCode Overrides

      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         int hash = 57;
         hash = 27 * hash * ID.GetHashCode();
         return hash;
      }
      #endregion
      
   }


SnmpManagersExcludedOidsId.cs
Code:
/// <summary>
   /// SnmpManagerExcludedOidsId object for NHibernate mapped table 'snmp_manager_excluded_oids'.
   /// </summary>
   [Serializable]
    [DataContract]
   public class SnmpManagerExcludedOidsId
   {
      #region Member Variables
        protected int _managerId;
      protected string _oid;
      #endregion

      #region Constructors
         
      public SnmpManagerExcludedOidsId() {}

        public SnmpManagerExcludedOidsId(int managerId, string oid)
        {
            _managerId = managerId;
            _oid = oid;
        }
               
      #endregion

      #region Public Properties

        [DataMember]
        public virtual int ManagerId
      {
            get { return _managerId; }
            set { _managerId = value; }
      }

        [DataMember]
      public  virtual string Oid
      {
         get { return _oid; }
         set {
            if ( value != null && value.Length > 40)
               throw new ArgumentOutOfRangeException("value", value.ToString(), "Oid cannot contain more than 40 characters");
            _oid= value; }
      }
      #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;
         SnmpManagerExcludedOidsId castObj = (SnmpManagerExcludedOidsId)obj;
         return ( castObj != null ) &&
            this._managerId.Equals(castObj._managerId) &&
         this._oid == castObj.Oid;
      }
      /// <summary>
      /// local implementation of GetHashCode based on unique value members
      /// </summary>
      public override int GetHashCode()
      {
         int hash = 57;
         hash = 27 * hash * _managerId.GetHashCode();
         hash = 27 * hash * _oid.GetHashCode();
         return hash;
      }
      #endregion
      
   }


Name and version of the database you are using:

SQL Server 2000 SP4

The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 8:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You've mapped your collection as the inverse part of the association:

Code:
<bag name="SnmpManagerExcludedOids" inverse="true" lazy="true" cascade="all-delete-orphan">
      <key column="manager_id" />
      <one-to-many class="Digitalk.Gateway.Core.DomainObjects.SnmpManagerExcludedOids,Digitalk.Gateway.Core" />
    </bag>


But there's no many-to-one association on the child which is necessary as the owning end of the association. You've put the child end of the association into the composite id, which I think is not working. You can try to remove the inverse attribute on the collection and see if that helps.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 4:03 am 
Newbie

Joined: Mon Jul 28, 2008 10:16 am
Posts: 4
Hi,

Thanks for your reply. I have tried removing the inverse attribute but to no avail.

I think I may have something fundamentally wrong with my mappings. On my SnmpExcludedOids object should I have a many to one SnmpManager mapping?

Regards,

Paul Chapman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 5:07 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
On my SnmpExcludedOids object should I have a many to one SnmpManager mapping?


Not necessarily. Have a look at http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/example-parentchild.html. You find a detailed explanation about the differences between a bidirectional and a unidirectional association there.

And have a look here, there's are some real good basic informations about collection mapping:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/06/12/mapping-collections-in-nhibernate-part-1.aspx

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.