-->
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: Hibernate puts empty object as Map key
PostPosted: Wed Feb 09, 2005 1:11 pm 
Newbie

Joined: Thu Nov 04, 2004 10:24 am
Posts: 17
An object is loaded in a query, and it has a Map. The key of that Map has a hashcode calculated, as usual, on the basis of its business fields. But when Hibernates [i]puts an object in the Map, the key is empty! None of the fields except the persistence ID has been set, and so the hashcode is calculated as zero. Later, when we try to get an object from the Map, the key has a different hashcode, and so the lookup fails.

We are using Hibernate 2.1.6, and the code is in

Code:
net.sf.hibernate.collection.Map
//...line 202, code trimmed for clarity
   public Object readFrom(....){...
      Object index = persister.readIndex( rs, getSession() );
// the index (key) is hollow, and so map.put uses a zero hashcode!
      map.put(index, element);
      return element;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 16, 2007 12:52 am 
Newbie

Joined: Thu Dec 08, 2005 9:57 am
Posts: 3
I have the same problem with Hibernate 3.2.3.ga.

For a Map-mapping with a non-trivial key (i.e. another hibernate managed
entity), the key along with its value is put into the map in 'hydrated' state,
e.g. with the business-id of the key set to null. It is only in the second pass,
where the key is filled, which breaks the Map-contract to not alter the
hashcode (which is depends on the buisness id) after an entry has been
added to a map/set.

A short (reduced) example:

Code:
public class Catalog {
    // ....
    @ManyToMany
    @MapKey(name = "type")
    @JoinTable(name = "catalog_group_rel",
               joinColumns = {@JoinColumn(name = "catalog_id")},
               inverseJoinColumns = {@JoinColumn(name = "group_id")})
    public Map<Type, ProductGroup> getProductGroups() {
        return productGroups;
    }
}


public class ProductGroup {
    // .....
    @ManyToOne
    @JoinColumn(name = "type", nullable = false)
    public Type getType() {
        return type;
    }
}

public class Type {
    // .....
    @Column (name = "name", nullable = false, unique = true)
    public String getName() {
        return name;
    }

    // .....
    @Override
    public int hashCode() {
        return (getName() != null) ? getName().hashCode() : 0;
    }
}


(My use case showing this behaviour contains some additional relations with
ProductGroup:Product = 1:N and Product:Type = N:1, but I don't think this is relevant here)

Is there a workaround to force hibernate to fill in the key completely before
building up the map ?


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.