[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-772) null in maps are handled inconsistently
Ferenc Gratzer (JIRA)
noreply at atlassian.com
Fri Apr 29 07:12:59 EDT 2011
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=42167#action_42167 ]
Ferenc Gratzer commented on HHH-772:
------------------------------------
I want to share how this old and insane hibernate issue could be circumvented fairly easy.
Let's take the next example:
public class Owner {
private Map<KeyClass, ValueClass> mapping = new HashMap<KeyClass, ValueClass>();
public ValueClass getMappingValue(KeyClass keyClass) {
return mapping.get(keyClass);
}
public void addMapping(KeyClass keyClass, ValueClass valueClass) {
mapping.put(keyClass, valueClass);
}
}
This wouldn't work if the value of the map is null.
owner.addMapping(keyClass, null)
1) Add a new dummy boolean column to the mapping table:
ALTER TABLE MAPPING_TABLE ADD DUMMY SMALLINT DEFAULT 0 NOT NULL;
2) Add a <composite-element> as the value of the map:
<map name="mapping" cascade="all" fetch="select" lazy="true" mutable="true" inverse="false" table="MAPPING_TABLE">
<cache usage="read-write" />
<key column="OWNER_ID" not-null="true" />
<map-key-many-to-many class="KeyClass" column="KEY_ID" />
<!-- This composite element is only created as Hibernate doesn't allow null as map value, see HHH-772 -->
<composite-element class="MapValueClass">
<many-to-one name="mapValue" column="VALUE_ID" class="ValueClass" />
<property name="dummy" column="DUMMY" not-null="true" type="boolean" />
</composite-element>
</map>
3) Create a MapValueClass class:
public class MapValueClass {
private ValueClass valueClass;
@SuppressWarnings("unused")
private boolean dummy;
@SuppressWarnings("unused")
private MapValueClass() {
// Hibernate
}
public MapValueClass(ValueClass valueClass) {
this.valueClass = valueClass;
}
public ValueClass getValueClass() {
return valueClass;
}
}
4) Change the mapping configuration in Owner class:
class Owner {
Map<KeyClass, MapValueClass> mapping = new HashMap<KeyClass, MapValueClass>();
public ValueClass getMappingValue(KeyClass keyClass) {
return mapping.get(keyClass).getValueClass();
}
public void addMapping(KeyClass keyClass, ValueClass valueClass) {
mapping.put(keyClass, new MapValueClass(valueClass));
}
}
Now the owner.addMapping(keyObject, null) works fine.
> 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