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 ?