-->
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.  [ 6 posts ] 
Author Message
 Post subject: issue with hashmap of entity classes
PostPosted: Sun Feb 15, 2004 3:20 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I have a Profile object, that I would like to contain a hashmap of ProfilePreference objects, where the hashmap key, is an identifier for the preference_id- located in a lookup table.

When creating the profile initially, Hibernate does a fine job of inserting records for the map, and reading them back. However, when I update one of the objects in the map, hibernate doesn't seem to notice the changes to the child "PRofilePreference" object. Cascade is all-delete-orphan.

Here's some snippets of relevent code-

The map declaration on the profile object in mapping file:
Code:
<map name="profilePreferences" table="PROFILE_PREFERENCE" lazy="true" cascade="all-delete-orphan" >
          <key column="profile_id"/>
          <index column="profile_preference_type_code" type="java.lang.Integer"/>
         <one-to-many class="com.housing.domain.ProfilePreference"  />
      </map>

ProfilePreference class (the one in the hashmap)

<class name="com.housing.domain.ProfilePreference" table="PROFILE_PREFERENCE" >

      <id name="id" type="java.lang.Integer" column="ID">
         <generator class="identity" />
      </id>

      <property name="value" type="java.lang.String" column="VALUE" not-null="true" length="50" />
      <property name="creationTimestamp" type="java.sql.Date" column="CREATION_TIMESTAMP" not-null="true" length="19" />
      <property name="modTimestamp" type="java.sql.Date" column="MOD_TIMESTAMP" length="19" />
      <property name="updateProfile" type="java.lang.Integer" column="UPDATE_PROFILE" not-null="true" length="11" />
      <property name="profilePreferenceType" type="java.lang.Integer" column = "PROFILE_PREFERENCE_TYPE_CODE" not-null="true" />
      <!-- associations -->
      <!-- bi-directional many-to-one association to Profile -->
      <many-to-one name="profile" class="com.housing.domain.Profile" not-null="true">
         <column name="PROFILE_ID" />
      </many-to-one>


update code from the parent "Profile" object:
ProfilePreference pref = (ProfilePreference)this.profilePreferences.get(new Integer(prefValues[i].getLabel()));
         if (pref != null)
         {
            profilePreferences.remove(new Integer(prefValues[i].getLabel()));
            pref.setValue(prefValues[i].getValue());
            pref.setModTimestamp(now);
            pref.setUpdateProfile(updater.getId());
            pref.setProfile(this);
            profilePreferences.put(new Integer(prefValues[i].getLabel()), pref);
         }





Thanks!
James


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 6:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please note that Hibernate does not support bidirectional one-to-many associations with an indexed collection (list, map or array) as the "many" end. You may use it but you'll have to manage the index in the many side as an explicit mapping and set the one side as inverse="true"

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 7:55 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Thanks for your reply- you guys are great!

If the underlying datastore has a key constraint (such that parent must be created before the child can be in the db), then the docs seem to indicate that a bidirectional relationship is necessary.

Are you saying that my code should work, provided that I set inverse=true on the parent, although it will be unsupported?
I understand in the child object I will need to manually maintain the index (I've used lists before where this was necessary too)
I tried your suggestion, but HIbernate still does not seem to be "seeing " changes to the dependent collection.

Oh yeah- i'm uising hib 2.1.1

Thanks again!
James


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 3:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This should work
Are you sure you update both sides as shown in the Parent Child relationship chapter ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 9:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There is allready an example for explicit index management in the forum, take a look: http://forum.hibernate.org/viewtopic.php?t=926946&highlight=mapped+index+list+child


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 10:31 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Thanks all. You're suggestions worked. I now have a hashmap of entity classes from my parent object.

I kept the relationship bidirectional, and am manually updating the child object rolumn which is also the index, or hashmap key.

--James


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