[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-772) null in maps are handled inconsistently

Nelson Murphy (JIRA) noreply at atlassian.com
Fri Feb 5 14:36:31 EST 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=35487#action_35487 ] 

Nelson Murphy commented on HHH-772:
-----------------------------------

In case this is useful to anyone else, I ended up forking the Hibernate core (v3.3.1.GA) and modifying PersistentMap to handle null values.

{code:title=PersistentMap.java.ApplyTo.3.3.1.svn.patch|borderStyle=solid}
@@ -182,12 +182,15 @@
 			}
 		}
 		initialize( true );
+		boolean newKey = !map.containsKey( key );
 		Object old = map.put( key, value );
 		// would be better to use the element-type to determine
 		// whether the old and the new are equal here; the problem being
 		// we do not necessarily have access to the element type in all
 		// cases
-		if ( value != old ) {
+		if ( newKey
+		        || value != old
+		        || ( (old==null) != (value==null) ) ) {
 			dirty();
 		}
 		return old;
@@ -279,7 +282,7 @@
 	throws HibernateException, SQLException {
 		Object element = persister.readElement( rs, owner, descriptor.getSuffixedElementAliases(), getSession() );
 		Object index = persister.readIndex( rs, descriptor.getSuffixedIndexAliases(), getSession() );
-		if ( element!=null ) map.put(index, element);
+		map.put(index, element);
 		return element;
 	}
 
@@ -408,8 +411,12 @@
 		while ( iter.hasNext() ) {
 			Map.Entry e = (Map.Entry) iter.next();
 			Object key = e.getKey();
-			if ( e.getValue()!=null && map.get(key)==null ) {
-				deletes.add( indexIsFormula ? e.getValue() : key );
+			if ( !map.containsKey(key) ) {
+			    if ( !indexIsFormula ) {
+			        deletes.add( key );
+			    } else if ( e.getValue() != null ) {
+                    deletes.add( e.getValue() );
+                }
 			}
 		}
 		return deletes.iterator();
@@ -419,18 +426,17 @@
 	throws HibernateException {
 		final Map sn = (Map) getSnapshot();
 		Map.Entry e = (Map.Entry) entry;
-		return e.getValue()!=null && sn.get( e.getKey() )==null;
+		return !sn.containsKey( e.getKey() );
 	}
 
-	public boolean needsUpdating(Object entry, int i, Type elemType) 
-	throws HibernateException {
-		final Map sn = (Map) getSnapshot();
-		Map.Entry e = (Map.Entry) entry;
-		Object snValue = sn.get( e.getKey() );
-		return e.getValue()!=null &&
-			snValue!=null &&
-			elemType.isDirty( snValue, e.getValue(), getSession() );
-	}
+    public boolean needsUpdating(Object entry, int i, Type elemType) 
+    throws HibernateException {
+        final Map sn = (Map) getSnapshot();
+        Map.Entry e = (Map.Entry) entry;
+        Object snValue = sn.get( e.getKey() );
+        return sn.containsKey(e.getKey()) &&
+            elemType.isDirty( snValue, e.getValue(), getSession() );
+    }
 
 
 	public Object getIndex(Object entry, int i, CollectionPersister persister) {
@@ -457,7 +463,7 @@
 	}
 
 	public boolean entryExists(Object entry, int i) {
-		return ( (Map.Entry) entry ).getValue()!=null;
+		return map.containsKey( ((Map.Entry) entry).getKey() );
 	}
 
 	final class Clear implements DelayedOperation {
{code}

Again, this patch is for v3.3.1 from a year ago -- I haven't checked to see if the class has changed since then.

> null in maps are handled inconsistently
> ---------------------------------------
>
>                 Key: HHH-772
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-772
>             Project: Hibernate Core
>          Issue Type: Improvement
>          Components: core
>            Reporter: Max Rydahl Andersen
>             Fix For: 3.1 beta 1
>
>
> regarding case 00004729.
> group.getUsers().put("something", null);
> Does not result in any insert.
> Inserting "something", null manually into the underlying table and
> when hibernate reads the map will have "something"->null in the map.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list