(Originally posted to NHibernate by mistake, reposted here)
n the Hibernate documentation, in the Component Mapping section it states that:
Quote:
When reloading the containing object, Hibernate will assume that if all component columns are null, then the entire component is null.
Our code assumes that component values are never null. If components are assigned null values, this causes errors in a lot of places. Adjusting all code to trap and handle null component cases is not really desirable.
Currently the best workaround appears to be as described in
http://forum.hibernate.org/viewtopic.php?t=956508 ie to change the "getter" method for the component to create+return a new component object in the case where the component is currently null. This is a nasty hack however, and further causes problems as Hibernate then thinks that the object values have changed and will persist the object every time.
For now, I have locally adjusted the Hibernate code so that components are always initialised even if all component values are null. The code change is very simple, changing the final line of code in org.hibernate.type.ComponentType.hydrate(..) (previous code commented out):
Code:
// Always return the composite object, even if all values are null
return values;
//return notNull ? values : null;
}
Ideally, it should be possible to switch Hibernate's functionality so that we can choose between the two options (ie whether to set the component to null/not if all component columns are null) - either globally or per-component.
If one of the Hibernate development leads agreed with the change and gave me some pointers, I'd be happy to help with implementation/testing.
regards,
Ste