views:

24

answers:

1

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?

A: 

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.

Kurt Du Bois
My way of assigning properties is another right way to do so.
Candy Chiu