title: Does the Hibernate Query Cache work in Dom4J mode
body: <p><strong>Background</strong>:  Our project team is using Hibernate's dom4j entity mode to generate SOAP responses for a multiple client applications.  The amount of data being delivered is massive and response time is an issue.  We can significantly reduce the number of SQL calls by using the Hibernate Query Cache and/or the 2nd Level Cache.  Due to the architecture of our system, the 1st Level Cache (Session) level mechanism will not improve performance in the way that Query Cacheing at the SessionFactory works.  </p>
  
  <p><strong>Question</strong>:  The underlying question is whether Hibernate's DOM4J Entity mode is compatible with Query cacheing.  Query results are being put into the Query Cache.  However, when a <code>query.list()</code> method executes and a matching cached query is found, then the following exception is thrown:  </p>
  
  <p><strong>Caused by</strong>: </p>
  
  <pre><code>java.lang.ClassCastException: org.dom4j.tree.DefaultElement cannot be cast to java.math.BigDecimal
        at org.hibernate.type.descriptor.java.BigDecimalTypeDescriptor.extractHashCode(BigDecimalTypeDescriptor.java:36)
        at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:197)
        at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:192)
        at org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:126)
        at org.hibernate.engine.EntityKey.&lt;init&gt;(EntityKey.java:70)
        at org.hibernate.type.ManyToOneType.scheduleBatchLoadIfNeeded(ManyToOneType.java:160)
        at org.hibernate.type.ManyToOneType.beforeAssemble(ManyToOneType.java:246)
        at org.hibernate.cache.StandardQueryCache.get(StandardQueryCache.java:143)
        at org.hibernate.loader.Loader.getResultFromQueryCache(Loader.java:2361)
        at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2309)
        at org.hibernate.loader.Loader.list(Loader.java:2268)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
  </code></pre>
  
  <p>Our environment is Core-Spring/Hibernate...</p>
  
  <p>In our spring-hibernate session factory configuration, the following configuration is used:</p>
  
  <pre><code>&lt;prop key= "hibernate.cache.provider_class"&gt;net.sf.ehcache.hibernate.EhCacheProvider&lt;/prop&gt;
  &lt;prop key= "hibernate.cache.region.factory_class"&gt; net.sf.ehcache.hibernate.EhCacheRegionFactory&lt;/prop&gt;
  &lt;prop key="hibernate.cache.query.factory_class"&gt; org.hibernate.cache.StandardQueryCacheFactory&lt;/prop&gt;
  &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
  &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;
  &lt;prop key="hibernate.generate_statistics"&gt;true&lt;/prop&gt;
  &lt;prop key="hibernate.cache.use_structured_entries"&gt;true&lt;/prop&gt;
  </code></pre>
  
  <p><strong>Initial Analysis</strong>:  My gut feeling is that Hibernate's "experimental" DOM4J entity mode is not compatible with the Query and Entity Cache.  It is also important to note that our Hibernate hbm.xml mapping files using dynamic mapping.  That is, the mapping files do not have Class references; instead they have entity references.  Thus, the XML responses get generated directly without populating a class's objects.  </p>
  
  <p>I would be tremendously grateful for any assistance in this matter.  </p>