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):