-->
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.  [ 1 post ] 
Author Message
 Post subject: Bidirectional HashMap
PostPosted: Tue Jun 29, 2004 4:04 pm 
Beginner
Beginner

Joined: Tue Jun 29, 2004 3:44 pm
Posts: 43
I have a Parent class with a HashMap of Child classes. Child has a parent attribute to refer to its Parent.

I have successfully persisted the Map and both classes with Hibernate 2.1.3 to MySQL. I have had a good deal of trouble getting the classes to load from the DB - the parent attribute of Child is not set. It is set before saving when it is added to the Parent. The objects are working, I'm just having trouble persisting and retrieving them.

I have tried a couple of things, and have only been able to get it to work by changing the Java code in the setMap method of Parent to iterate through the map and assign parent values to each Child. I'm assuming, though, that there must be a better way to do this. A way that doesn't rely on this iteration.

I'll post the mapping for the two classes that came closest without having to iterate through the map. When done like this, a parent id is stored by the child, but it's always the same number - and never an actual primary key of any parent. I've no idea where the number comes from, though it's always the same.

Many thanks for any help or suggestions.

mapping for the Parent (Party):
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   
   <class name="parties.Party" table="party">
      <id name="id" type="string" column="id">
         <generator class="assigned"/>
      </id>
      <discriminator column="type"/>
      
      <!-- timestamp allows updating with assigned id -->
      <timestamp name="lastSave" column="timestamp" unsaved-value="null"/>
      
      <map
         name="childMap"
         table="child_map"
         cascade="all-delete-orphan">
                  
         <key column="owner_id"/>
         <index-many-to-many column="key_id" class="parties.ChildType"/>
         <many-to-many column="element_id" class="parties.Child"/>
      </map>


      <subclass name="parties.Person"
         discriminator-value="person">   
      </subclass>
      
      <subclass name=.parties.Organization"
         discriminator-value="organization">
      </subclass>
      
            
   </class>
</hibernate-mapping>


mapping for child:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   
   <class name="parties.Child"
      table="child">
      
   
      <id name="id" type="string" column="id">
         <generator class="uuid.hex"/>
      </id>
      
      <many-to-one name="party"
         column="party_id" not-null="true"/>
      
   
   </class>
</hibernate-mapping>
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.