Why does Hibernate 2nd level cache only cache queries within a session?

Posted by Synesso on Stack Overflow See other posts from Stack Overflow or by Synesso
Published on 2010-04-12T05:25:16Z Indexed on 2010/04/12 5:33 UTC
Read the original article Hit count: 255

Filed under:
|
|
|

Using a named query in our application and with ehcache as the provider, it seems that the query results are tied to the session within the cache. Any attempt to access the value from the cache for a second time results in a LazyInitializationException

We have set lazy = true for the following mapping because this object is also used by another part of the system which does not require the reference... and we want to keep it lean.

<class name="domain.ReferenceAdPoint" table="ad_point" mutable="false" lazy="false">
    <cache usage="read-only"/>
        <id name="code" type="long" column="ad_point_id">
            <generator class="assigned" />
        </id>
        <property name="name" column="ad_point_description" type="string"/>
        <set name="synonyms" table="ad_point_synonym" cascade="all-delete-orphan" lazy="true">
            <cache usage="read-only"/>
            <key column="ad_point_id" />
            <element type="string" column="synonym_description" />
        </set>
</class>

<query name="find.adpoints.by.heading">from ReferenceAdPoint adpoint left outer join fetch adpoint.synonyms where adpoint.adPointField.headingCode = ?</query>

Here's a snippet from our hibernate.cfg.xml

<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>

It doesn't seem to make sense that the cache would be constrained to the session. Why are the cached queries not usable outside of the (relatively short-lived) sessions?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about ehcache