I am using Spring to create the SessionFactory:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="mappingResources">
<list>
<value>META-INF/mapping/domain-objects.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
I'd like to map one of the classes as read-only.
<class name="MyDomainObject">
<!-- everything works without this line -->
<cache usage="read-only" />
<id name="id" />
<property name="name"
column="name" />
</class>
After I added the caching strategy read-only to the mapping of MyDomainObject, the test program starts to throw exception:
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
I tried setting hibernate.cache开发者_运维知识库.use_second_level_cache and/or hibernate.cache.use_query_cache to true.
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
</value>
</property>
None of the options worked. What else do I need to use the read-only caching strategy?
The way you assign the properties in spring is incorrect.
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="SecondLevelCacheEnabled">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
</props>
</property>
This is the way you should be setting those properties.
Seeting the hibernate.cache.use_second_level_cache
to true
is not enough. You also have to define the Hibernate cache provider class and cache region factory to use. E.g.:
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.region.factory_class=org.hibernate.cache.SingletonEhCacheRegionFactory
精彩评论