-->
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.  [ 2 posts ] 
Author Message
 Post subject: Map not finding a key
PostPosted: Mon Nov 05, 2007 5:29 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
Hibernate version: 3.2.

Mapping documents:
Code:
// Map Snippet of AppliedPolicy
      <map name="keyValues" table="applied_policy_values" lazy="false" cascade="all">
         <key column="applied_policy_id"
            foreign-key="appl_values_appl_policy_fk"/>
         <map-key-many-to-many
              column="policy_simple_value_id"
              class="PolicySimpleValue"
              foreign-key="appl_values_value_fk"
            />
         <element column="value" type="text"></element>         
      </map>


Code:
   <class name="PolicySimpleValue" table="policy_simple_value" proxy="PolicySimpleValue">
      <id name="name" type="string" access="field"  unsaved-value="null" >
         <column name="name" length="50"/>
         <generator class="assigned"/>
      </id>
      
      <version name="version" column="version" access="field"/>
      
      <property name="description" column="description" type="text"/>
      <property name="type" column="value_type" type="string" length="10" access="field"/>
      

      <set name="policies" inverse="true" table="policy_attributes" lazy="true">
         <key column="policy_simple_value_id" />
         <many-to-many column="policy_id" class="Policy" />
      </set>

   </class>


Classes
Code:
class AppliedPolicy {
   .......
   public Map<PolicySimpleValue,String> getValues()
   public void setValues(Map<PolicySimpleValue,String> map)()
}

class PolicySimpleValue {
   // Assigned Id
   // equals and hashCode are implemented using
   // this attribute only...
   String name;

   // Other attributes
   String descripcion;
   int  version;
   ...........
}


The problem or question

1 - I fetch all PolicySimpleValue objects in a transaction

2 - I fetch one AppliedPolicy with all its nested elements
in another transaction

3 - All transactions closed, no hibernate session opened,
trying to find a key in the map:

Now I am going to try to find PolicySimpleValue with
name "A" in the AppliedPolicy object (of course there is
one with the value "X" associated).

If I use the PolicySimpleValue retrieved in step 1, the value
returned is null although there is a PolicySimpleValue named "A" as
a key int the map.

If I iterate over key-values of the retrieved AppliedPolicy map
I can see a PolicySimpleValue with name "A", if I use this
object as a key to the map I can find the correct value in this
case "X".

I tried a similar case but with a PolicySimpleValue SET, just
asking if the set contains my PolicySimpleValue and it works OK.

Of course I can iterate over the map keys asking for every policy name
and find my value, but I really suppose that is not the idea when using a MAP.

I'm not sure what I'm doing wrong, could anyone please give me some
help.

Thanks in advance
tonio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 7:22 pm 
Beginner
Beginner

Joined: Thu Jan 11, 2007 7:19 pm
Posts: 20
Location: Argentina, Buenos Aires
There is really a simple solution, (really stupid from me).

My hash/equals method were the Eclipse's default implementation.

Well I have to correct 2 things:

1 - Instead of using attribute access, use get getter method, if not
hibernate proxy will no be called

2- The class of hibernate fetched object is a proxy and not
the original bean class, so you must decide what classes
will be acceptable for the equals method.



Hope at least useful for someone,
thanks to anyone who have just taken the time
to read my e-mail.


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