I'm having trouble initializing a (non-lazy) sorted set when the second level cache is enabled.
I have a User object that contains a SortedSet of EmailAccount objects. When accessing the user, with the second level cache enabled, the set appears to be in the incorrect order. The EmailAccount objects implement Comparable by comparing creation timestamps. Using some debugging, the following sequence of method calls happen during a test of a user with three (already persisted) email accounts (ea1, ea2, ea3):
ea1.setId()
ea2.setId()
ea2.setCreationTimestamp()
ea3.setId()
ea3.setCreationTimestemp()
ea2.compareTo(ea3)
ea1.compareTo(ea3)
ea1.compareTo(ea2)
ea1.setCreationTimestamp()
The comparisons are incorrect because ea1 has not had its creation timestamp set from the persistent data before it is used to compare the object.
When I disable the second-level cache, the objects are fully initialized before they are compared and the problem disappears.
I'm using Hibernate 3.2.5, MySQL 5.0.37.
Here's (hopefully) the relevant portions of the mapping docs:
Code:
<class name="mypackage.User" table="Users" lazy="false">
<cache usage="transactional"/>
...
<set name="emailAccounts" cascade="all-delete-orphan" lazy="false" sort="natural">
<cache usage="transactional"/>
<key column="user_id" not-null="true"/>
<one-to-many class="mypackage.EmailAccount"/>
</set>
</class>
...
<class name="mypackage.EmailAccount" table="EmailAccounts" lazy="false">
<cache usage="transactional"/>
<id name="id" type="long" column="EMAIL_ACCT_ID" unsaved-value="0" >
<generator class="native"/>
</id>
<property name="creationTimestamp" type="timestamp"/>
...
</class>
Can anyone shed some light on this situation?
Thanks!