views:

93

answers:

1

Does any one know, how to configure cache for hibernate with jboss ?

My clear question is I am using JPA and Jboss. Every time I call JPA method its creating entity and binding query.

My persistence properties are

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.cache.provider_class"   value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>

And I am creating entity manager the way shown below:

emf = Persistence.createEntityManagerFactory("pu");
em = emf.createEntityManager();
em = Persistence.createEntityManagerFactory("pu")
                        .createEntityManager();

Is there any nice way to manage entity manager resource insted create new every time or any property can set in persistance. Remember it's JPA.

A: 

The question is not clear, there are many second level cache providers for Hibernate and they are not application server specific.

To enable the second level cache, you need to set the following properties in Hibernate configuration file hibernate.cfg.xml:

<property name="hibernate.cache.use_second_level_cache">true</property>

And if you want to also enable query result caching:

<property name="hibernate.cache.use_query_cache">true</property>

Then, declare the name of a class that implements org.hibernate.cache.CacheProvider - a cache provider - under the hibernate.cache.provider_class property. For example, to use JBoss Cache 2:

<property name="hibernate.cache.provider_class">org.hibernate.cache.jbc2.JBossCacheRegionFactory</property>

Of course, the JAR for the provider must be added to the application classpath.

That's for the Hibernate side. Depending on the chosen cache provider, there might be additional configuration steps. But as I said, there are many second level cache providers: EHCache, JBoss Cache, Infinispan, Hazelcast, Coherence, GigaSpace, etc.

Pascal Thivent