hibernate 2.1.1
postgres 7.3.4
ehcache 0.7
I've got the relationship:
State -- *UsHouseDistrict
Here's the mapping:
Code:
<hibernate-mapping>
<class
name="District"
table="District"
batch-size="10"
>
<id
name="id"
unsaved-value="null"
>
<generator class="seqhilo"/>
</id>
<joined-subclass
name="State"
table="vgt_state"
>
<key column="id"/>
<set
name="usHouseDistricts"
lazy="true"
inverse="true"
cascade="none"
sort="natural"
>
<key
column="state_id"
>
</key>
<one-to-many
class="UsHouseDistrict"
/>
</set>
</joined-subclass>
<joined-subclass
name="UsHouseDistrict"
table="vgt_ushouse_district"
>
<key column="id"/>
<many-to-one
name="state"
class="State"
column="state_id"
not-null="false"
unique="false"
cascade="none"
outer-join="auto"
/>
</joined-subclass>
</class>
</hibernate-mapping>
Note that the UsHouseDistrict's State may be null and that a State's usHouseDistricts are held in a sorted set.
After I started caching the collection State.usHouseDistricts I started getting a NPE when the house districts were being assembled from the cache into the sorted set. I found that the compareTo() method used by UsHouseDistrict assumed that the state was not-null. In fact, all the existing house districts had a not null state even though this was not enforced in the db schema or the hibernate mapping.
So the question is ... is this how hibernate caching works? -- to, on fetching from the cache, only fill in the not-null (i.e. necessary) fields before possibly invoking a compareTo() / hashCode() / equals() operation? Related: will changing the relationship to not-null="true" fix the problem? Or have I found a bug? Is this at all related to the joined-subclass particulars of my mapping?