-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-one and derived objects
PostPosted: Tue Apr 11, 2006 1:50 pm 
Newbie

Joined: Tue Apr 11, 2006 12:18 pm
Posts: 3
Location: Portsmouth, NH, USA
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)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:48 pm 
Newbie

Joined: Fri Jun 30, 2006 5:02 pm
Posts: 7
Location: Vista, CA
Hi, I'm having the same issue with many-to-one and subclasses. Though I'm simply trying to save a new set of objects.

Unfortunately I have found some "issues" in the Hibernate core issue tracking system that seem to relate to our scenario.

See this one (which the description most closely matches my scenario, and I believe yours too, but is a duplicate of another issue)
http://opensource.atlassian.com/project ... se/HHH-667

The root "issue" is still open, and has an expected fixed version of 3.2.2... whenever that would be.
http://opensource.atlassian.com/project ... se/HHH-422

There's several comments on the various issues that have me somewhat confused as to the state of the issue, and if there are any work-arounds.

This seems like a major issue. I have no work-around at this point, but would be very interested to know if you've come up with anything.

Also, does anyone on the Hibernate team, or anyone else who's had this issue know of a fix or work-around?

Thanks,
Thunder


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 2:01 pm 
Newbie

Joined: Tue Apr 11, 2006 12:18 pm
Posts: 3
Location: Portsmouth, NH, USA
My resolution was to drop Hibernate altogether and use SQL. Essentially, I have an object / relational map similar to that of hibernate, but it allows me to specify the relationship like so:

<primary-key name="physicalport_pk">
<key column="id"/>
</primary-key>
<foreign-key name="physicalport_deviceid_fk" tableref="NetworkElement"
match="simple" onupdate="no action" ondelete="cascade">
<key column="deviceId" columnref="id"/>
</foreign-key>

I wrote a simple java class to parse the constraint relationships & create the SQL from them. Now, I use the same reflection hibernate uses to update data objects (via standard java reflection), and a single set of base methods to handle all insert / update / select calls.

I found hibernate to be useful for simple cases, but it couldn't handle the more complex relationships easily. So, I replaced it with my own solution. What's funny is that it took me less time to roll the generic SQL methods & ORM xml file than to try to figure out why I couldn't get Hibernate to work. :-)


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