Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I am having trouble mapping ports to an associated device in order to allow the cascade on delete to work. The basic object hierarchy is:
NetworkElement ------------- 1..* - PhysicalPort
|
DeviceA, DeviceB, DeviceC (derived from NetworkElement)
I'm using the table for class hierarchy approach, and I want to use a many-to-one relationship between the PhysicalPorts and their associated NetworkElement (or derived class). I don't want the NetworkElement class to contain a member variable list of PhysicalPorts - they are simply associated via the deviceId member variable in the PhysicalPort object.
If I attempt to add PhysicalPort objects that are associated with subclasses of NetworkElement (in this case a PBG), I get the "Instance not of expected entity type".
I've poured over the hibernate docs, examples, etc, but none of them use an association via derivation where a set isn't used within the NetworkElement type class. I've also tried using entity-name instead of the class name without positive results.
I'm assuming that there is some way to tell Hibernate "this and all subclasses", but I can't find the symantic in any of the docs or examples dealing with relationships.
I could just change the many-to-one relationship to a property and manually remove the PhysicalPorts via a HQL delete where clause, but that's not as efficient as having the DB do it automatically.
Any help and/or pointers will be greatly appreciated.
Hibernate version: 3.1
Name and version of the database you are using: PostgreSQL 8.0.3 on Windows XP.
Mapping documents:
<hibernate-mapping>
<class name="org.mg.core.data.PhysicalPort" table="PhysicalPort">
<id name="id" column="id">
<generator class="native"/>
</id>
<timestamp name="timestamp" column="modified"
unsaved-value="undefined" source="vm" generated="never"/>
<many-to-one name="deviceId" class="org.mg.core.data.NetworkElement"
column="deviceId" update="false" not-null="true"
unique="false" cascade="delete"
foreign-key="FK1_PORT_DEVICE_ID"/>
<!-- Other properties omitted since not relevant -->
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="org.mg.core.data.NetworkElement" table="NetworkElement"
discriminator-value="Manogia.NetworkElement">
<id name="id" column="deviceId">
<generator class="native"/>
</id>
<discriminator column="deviceType" type="string" length="255"/>
<timestamp name="timestamp" column="modified"
unsaved-value="undefined" source="vm" generated="never"/>
<property name="addedTimeStamp" type="long" column="added"/>
<!-- Other properties omitted since not relevant -->
</class>
</hibernate-mapping>
The subclasses all look like:
<subclass name="com.pannaway.pbg.core.data.PBG"
extends="org.mg.core.data.NetworkElement"
discriminator-value="Pannaway.PBG"/>
Full stack trace of any exception that occurs:
2006-04-11 11:04:59,641 80999 ERROR [org.mg.ds.server.adapters.jdbc.PhysicalPortAdapter] (NEExplorerPool_0:) PhysicalPort Add Failed:
org.hibernate.HibernateException: instance not of expected entity type: org.mg.core.data.NetworkElement
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3298)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1249)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:524)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:510)
at org.mg.ds.server.adapters.jdbc.PhysicalPortAdapter.add(PhysicalPortAdapter.java:57)