Composite Layer Cache Identifier

I am trying to cache an object in the second level cache of Hibernate, which has a composite identifier displayed in my persistence mapping file. Logs say that the first time a query is run, the class displayed as a composite identifier is cached. However, when I run the request a second time, the object is not retrieved from the cache. Instead, it starts the query again.

Is there a problem with Hibernate with complex second level cache identifiers?

Relevant Information:

  • Using Hibernate 3.1, ehcache 2.4.2
  • Composite ID class implements serializable
  • I am using a new Hibernate session when re-executing a request
  • I use hibernateTemplate.load (Class, ID) to retrieve an object

This is how I build my identifier and execute my query:

CompositeId id = new CompositeId(date, sessionId); UserDetails user = (UserDetails) hibernateTemplate.load(UserDetails.class, id); 

And here is how my persistence mapping file defines above:

 <class name="com.entities.UserDetails" table="USER_DETAILS" lazy="false"> <cache usage="read-write"/> <composite-id name="userId" class="com.entities.CompositeId" unsaved-value="undefined"> <key-property name="userSessionId" column="SESSION_ID" /> <key-property name="dateCreated" column="DATE_CREATED" type="date" /> </composite-id> 

EDIT: the plot is thickening ....

When I changed this policy to a read-only cache version, it worked fine. The behavior of a transactional cache seems extremely unpredictable. Can someone explain why this happened with read-write cache but works fine with read-only? This table is not updated, so I'm not sure why transactional semantics will make a difference in this case.

+8
hibernate composite-primary-key ehcache second-level-cache
source share
1 answer

This is similar to the error message with Hibernate . It looks like you can successfully use the cache as a workaround if you use the same instance of your composite key, and not the one that is equal to .equals() .

There is also a patch in the bug report, you can apply it yourself and collapse your own fixed Hibernate build.

0
source share

All Articles