-->
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: book 'Java Persistence With Hibernate' page 143 - FAQ
PostPosted: Wed Nov 29, 2006 6:05 pm 
Newbie

Joined: Wed Nov 29, 2006 5:53 pm
Posts: 1
In the FAQ on this page its stated -
Quote:
However, item1 and item2 are Java Map instances,
and the equals() implementation of a map is based on the key sets of
the map.


This statement is false cos in JDK5 AbstractMap.java implements equals thus

Code:
public boolean equals(Object o) {
   if (o == this)
       return true;
   if (!(o instanceof Map))
       return false;
   Map<K,V> t = (Map<K,V>) o;
   if (t.size() != size())
       return false;
        try {
            Iterator<Entry<K,V>> i = entrySet().iterator();
            while (i.hasNext()) {
                Entry<K,V> e = i.next();
      K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(t.get(key)==null && t.containsKey(key)))
                        return false;
                } else {
                    if (!value.equals(t.get(key)))
                        return false;
                }
            }
        } catch(ClassCastException unused) {
            return false;
        } catch(NullPointerException unused) {
            return false;
        }

   return true;
    }


So this implementation of equals does look at and compare values also(not just keys as stated).
Am i missing something here or not understanding what the author meant ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 6:21 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, you might be right and I might be wrong in what I said. In practice it doesn't work with two HashMap instances and the code in the examples. It looked to me like an equals() problem, but I only spend an hour or so on it.

If you can make the examples work, I'd be happy to post an errata.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.