-->
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.  [ 5 posts ] 
Author Message
 Post subject: mapping subclasses of a common parent to a single collection
PostPosted: Thu Jun 22, 2006 4:08 pm 
Newbie

Joined: Thu Jun 22, 2006 3:56 pm
Posts: 3
Hibernate version: Hibernate 3.2 cr2

Hi all - I've been reading through the manual and searching the web, but haven't yet found a solution to this. The situation is this, I have a class that maintains a collection, the elements of which are of differing subclasses of a common parent.

For example, say I have an EventManager class that maintains a collection of Event implementations. Now consider that Event is just an interface, or an otherwise bland parent class of several different implementations, say NormalEvent, BoringEvent, and ReallyCoolEvent. The EventManager maintains a heterogeneous collection of these subclasses. How do I map this? I've read about table per subclass et al, but haven't seen how to do what I'm talking about. How can this be done?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 7:46 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You haven't said how the subclass objects are stored in the DB, and there are several ways of doing this. But essentially, you map all four Event classes (Event and its three subclasses), and tell hibernate that Event is the superclass. Refdocs section 9 explains the options and what to do. Then when mapping the Event collection in EventManager, you use class="Event" rather than any of the subclasses. When you call eventManager.getEvents(), you'll get back a Collection<Event>, and each item in the Collection can be cast to the correct Event subclass, or used as a simple Event.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 11:54 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Hi,

It's not exactly a Hibernate solution, but what if you created a view which combined the different types of events and then mapped the view?

Good luck,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 2:05 pm 
Newbie

Joined: Thu Jun 22, 2006 3:56 pm
Posts: 3
thanks tenwit - I had read that section but didnt see the part that said to map to the superclass, be it an interface or abstract, etc. So that part is working, but now I'm getting an org.hibernate.MappingException when mapping a static inner class as a property of one of the subclasses. I'm not sure why since that's legal, and since i'm also successfully mapping 3 static inner enumeration types to another subclass. Here is my mapping with comments on the properties:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class
      name="net.godcode.biocycle.cycle.AbstractObservation"
      table="Observation"
   >
      <id name="id" type="long" column="observation_id">
         <generator class="native"/>
      </id>
      
      <joined-subclass
         name="net.godcode.biocycle.cycle.manager.persistent.HThermalObservation"
         table="HThermalObservation"
      >
         <key column="observation_id"/>
         <!-- this causes a mapping exception -->
         <property name="temperature"
            type="net.godcode.biocycle.cycle.ThermalObservation$Temperature"/>
      </joined-subclass>
      
      <joined-subclass
         name="net.godcode.biocycle.cycle.manager.persistent.HCervicalObservation"
         table="HCervicalObservation"
      >
         <key column="observation_id"/>
         <!-- these map successfully -->
         <property name="openness"
            type="net.godcode.biocycle.cycle.CervicalObservation$Openness"/>
         <property name="firmness"
            type="net.godcode.biocycle.cycle.CervicalObservation$Firmness"/>
         <property name="position" column="cposition"
            type="net.godcode.biocycle.cycle.CervicalObservation$Position"/>
      </joined-subclass>
      
   </class>
</hibernate-mapping>


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 24, 2006 6:13 pm 
Newbie

Joined: Thu Jun 22, 2006 3:56 pm
Posts: 3
got it :) - i described it in the mapping as a component. thanks again


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