FYI:
I just caught an interesting bug in my code, ultimately caused by Hibernate.
A
List field (mapped as a
list of
elements) was not getting written to DB.
It turns out that during
save(), Hibernate uses reflection to call setter methods on fields of
List type (likewise for other
Collection), so the original
List field is replaced by a special Hibernate
List, which wraps the original one. Unfortunately, our setter method did not just assign to the field as one might expect, but rather cleared the existing List and added elements to it, thus:
Code:
setMyListField(List list){
myListField.clear()
myListField.addAll(list);
}
This was done to avoid referential integrity problems, as we don't want to share a reference to the parameter.
So, when Hibernate called the setter, it
cleared not only the underlying field, but the parameter (Hibernate's
List) as well -- since it wraps the underlying field!
Tricky, that one. We'll need to keep our eyes open for similar problems wherever a setter or getter does more than simply set/get a field.