1

Background: 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.

Question: 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 query.list() method executes and a matching cached query is found, then the following exception is thrown:

Caused by:

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.<init>(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)

Our environment is Core-Spring/Hibernate...

In our spring-hibernate session factory configuration, the following configuration is used:

<prop key= "hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key= "hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.query.factory_class"> org.hibernate.cache.StandardQueryCacheFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>

Initial Analysis: 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.

I would be tremendously grateful for any assistance in this matter.

4

1 回答 1

0

所以,经过多次尝试,我发现了问题所在。

上面的初步分析是关键。当代码以动态映射模式生成 XML 时,ehcache 水合机制不起作用。解决该问题的解决方案是创建一个类文件来支持休眠映射。也就是说,当您使用带有指向类的名称属性的休眠映射 (hbm.xml) 文件时,一切都很好。Hibernate 可以对类进行水合,然后以 dom4j 实体模式生成 XML。

我在类标签中使用了一个实体名称属性。我的代码试图直接对 XML 进行水合。如上所示,这失败了。

所以,我希望这个解决方案有人一段时间。

于 2012-02-25T06:04:18.703 回答