Problem: I added a one-to-many relationship, using a Generic Collection (IList) that worked in other scenarios (not with the same Domain Model object but using the same generic collection, similar mapping structure, etc.) but I get an InvalidCastException exception saying that it's "Unable to cast object of type 'System.Collections.Generic.List`1[SubInterface]' to type 'Iesi.Collections.ISet'."
When I remove the <set name="SubInterfaces"> <key column="FK_ParentNetworkInterfaceID"/> <one-to-many class="SubInterface, Domain"/> </set>
from PhysicalNetworkInterface in the mapping file (see below - bolded), I don't get the error. I tried a number of things to get it to work -- thought it was due to a virtual property in SubInterface & removed that & still had the error. I replaced System.Collections.Generic.IList<SubInterface> with System.Collections.IList and set it to an ArrayList and still received an error (of course, the exception then said that it couldn't cast object of type 'System.Collection.ArrayList').
is the object hierarchy (which has collections of other objects in the same hierarchy) and the way I'm using the db table ok?
Thanks very much for any help -- I provided the detail and bolded the pertinent areas for focus --
Bill
Hibernate version: 1.2.0.GA merged with the NH-466 patch
Object Model (I also show the C# classes in another section):
(PhysicalNetworkDevice is shown here just for FYI -- it's not used in this test scenario)
* PhysicalNetworkDevice - associations: 1-M PhysicalNetworkInterface; 1-M VirtualNetworkInterface
Following is an inheritance hierarchy that also shows the associations for each class (each asterisk represents the level - NetworkInterface is the root object of the hierarchy; PhysicalNetworkInterface and VirtualNetworkInterface are its immediate subclasses; SubInterface and VirtualNetworkInterfaceInDevice are subclasses of VirtualNetworkInterface; Loopback, VLAN, and Tunnel are subclasses of VirtualNetworkInterfaceInDevice):
* NetworkInterface (abstract)
* * PhysicalNetworkInterface- associations: 1-M SubInterface; M-1 PhysicalNetworkDevice
* * VirtualNetworkInterface (abstract)
* * * SubInterface - associations: M-1 PhysicalNetworkInterface
* * * VirtualNetworkInterfaceInDevice (abstract)
* * * * Loopback
* * * * VLAN
* * * * Tunnel
Mapping documents:
(not showing relationship with PhysicalNetworkDevice b/c it's not used for this test)
(Please refer to the object model in case the lack of indentation in the object hierarchy doesn't present the inheritance hierarchy well)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="NetworkInterface, Domain" table="dbo.tblNetworkInterface">
<id type="Int32" column="ID" name="ID" unsaved-value="null">
<generator class="identity"></generator>
</id>
<discriminator column="TypeName" />
<!-- Not showing simple <property> and <component> elements -->
<subclass name="PhysicalNetworkInterface, Domain" discriminator-value="PhysicalNetworkInterface">
<set name="SubInterfaces"> <key column="FK_ParentNetworkInterfaceID"/> <one-to-many class="SubInterface, Domain"/> </set>
<many-to-one name="ParentDevice" class="PhysicalNetworkDevice, Domain" column="FK_DeviceID" outer-join="false" cascade="all"/>
</subclass>
<subclass name="VirtualNetworkInterface, Domain" discriminator-value="VirtualNetworkInterface">
<subclass name="SubInterface, Domain" discriminator-value="SubInterface"> <many-to-one name="ParentPhysicalNetworkInterface" class="PhysicalNetworkInterface, Domain" column="FK_ParentNetworkInterfaceID" outer-join="true" cascade="all"/> </subclass>
<subclass name="VirtualNetworkInterfaceInDevice, Domain" discriminator-value="VirtualNetworkInterfaceInDevice">
<subclass name="Loopback,Domain" discriminator-value="Loopback">
</subclass>
<subclass name="Tunnel, Domain" discriminator-value="Tunnel">
</subclass>
<subclass name="VLAN, Domain" discriminator-value="VLAN">
</subclass>
</subclass>
</subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
SubInterface subInterface = new SubInterface();
// Set simple Properties
. . .
PhysicalNetworkInterface physicalNetworkInterface = new PhysicalNetworkInterface();
// Set simple Properties
. . .
// Set associations
subInterface.ParentPhysicalNetworkInterface = physicalNetworkInterface;
physicalNetworkInterface.SubInterfaces = new List<SubInterface>();
physicalNetworkInterface.SubInterfaces.Add(subInterface);
// Use nHib to save physicalNetworkInterface
ITransaction transaction = session.BeginTransaction();
session.Save(physicalNetworkInterface);
C# Classes
(not showing relationship with PhysicalNetworkDevice b/c it's not used for this test):
public abstract class NetworkInterface
{
public NetworkIntrface() {}
// common properties not shown; abstract methods not shown
}
public class PhysicalNetworkInterface : NetworkInterface
{
public PhysicalNetworkInterface() {}
public System.Collections.Generic.IList<SubInterface> SubInterfaces { get { return this._SubInterfaces; } set { _SubInterfaces = value; } }
private System.Collections.Generic.IList<SubInterface> _SubInterfaces;
// other methods (override abstract) not shown
}
public abstract class VirtualNetworkInterface : NetworkInterface
{
public VirtualNetworkInterface() {}
}
public class SubInterface : VirtualNetworkInterface
{
public SubInterface() {}
public PhysicalNetworkInterface ParentPhysicalNetworkInterface { get { return this._ParentPhysicalNetworkInterface; } set { this._ParentPhysicalNetworkInterface = value; } }
private PhysicalNetworkInterface _ParentPhysicalNetworkInterface;
}
public abstract class VirtualNetworkInterfaceInDevice : VirtualNetworkInterface
{
// not showing ParentDevice Property
}
public class Tunnel : VirtualNetworkInterfaceInDevice
{
public Tunnel() {}
}
public class VLAN : VirtualNetworkInterfaceInDevice
{
public VLAN() {}
}
public class Loopback : VirtualNetworkInterfaceInDevice
{
public Loopback() {}
}
Db Tables:
tblNetworkInterface:
* single table for all objects in the NetworkInterface hierarchy - table-per-class-hierarchy (TypeName is discriminator)
* FK_ParentNetworkInterfaceID implements the bidirectional relationship, PhysicalNetworkInterface 1-M SubInterface
Full stack trace of any exception that occurs:
(note: the code I presented in the section, Code between sessionFactory.openSession() and session.close(), has the raw code; the standard nHibernate stuff in encapsulated in other objects):
"at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at Netops.CHNOPS.DAL.nHib.DALNHibBase.dalFrameworkSaveUpdateDeleteHelper(Object o, DALPersistence settings, String methodName) in C:\\Projects\\NetOPS\\Netops.CHNOPS\\CHNOPSSolution\\Netops.CHNOPS\\Netops.CHNOPS.DAL\\nHib\\DALNHibBase.cs:line 177"
Name and version of the database you are using: Microsoft SQL Server 2005; Version: 9.00.1406.00; Product Level: RTM; Edition: Developer Edition
_________________ metazone
|