-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Does JPA have second-level caching support?
PostPosted: Thu Jan 18, 2007 7:21 am 
Newbie

Joined: Wed Dec 27, 2006 10:03 am
Posts: 19
Greetings,

My short question is: is there any JPA cache provider that would enable second-level caching in JPA application? Can EHCache\OSCache work with JPA?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 18, 2007 7:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hibernate EntityManager is implemented on top of Hibernate Core and Annotations so everythign that is available to hibernate core is available to JPA applications that uses Hibernate EntityManager.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Evicting entities from 2nd Level Cache
PostPosted: Sun Sep 30, 2007 3:01 pm 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
How can I evict some entity from 2nd Level Cache using JPA + Hibernate? Is there any method as below?

"For the second-level cache, there are methods defined on SessionFactory for evicting the cached state of an instance, entire class, collection instance or entire collection role.

sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class); //evict all Cats
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections"

How can I do that using EntityManager or EntityManagerFactory?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 6:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there is no concept of 2nd lvl cache in JPA spec.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 9:24 am 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
Even the JPA spec does not have the concept of 2nd level cache I can use it with Hibernate as JPA vendor.

I just have to pass the specific properties for cache at persistence.xml and it works.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
          version="1.0">

      <persistence-unit name="SpringJpaPersistenceUnit" transaction-type="RESOURCE_LOCAL">
   
      <!-- A list of vendor-specific properties -->
      <properties>
         <property name="hibernate.cache.use_second_level_cache" value="true"/>
         <property name="hibernate.cache.use_query_cache" value="true"/>
         <property name="hibernate.cache.use_minimal_puts" value="true"/>   
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
         <property name="hibernate.cache.provider_configuration_file_resource_path" value="META-INF/ehcache.xml"/>         
      </properties>
   </persistence-unit>
</persistence>

I just want to know how to evict the objects cached... Should I do that directly using the Cache API (in my case EhCache API)? I guess this is an ugly solution...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 9:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
You use an hibernate specific feature that is not available in JPA api so yes you have to use hibernate interfaces/classes for this.

That means:

sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class); //evict all Cats
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections"

if you start using ehcache api you will end up with an inconsistent cache.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 12:38 pm 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
Is there any better way to access sessionFactory instance than below?

I would be useful if the interface HibernateEntityManager would have some getSessionFactory method...

Code:
@PersistenceContext
protected EntityManager entityManager;
...
HibernateEntityManager hibernateEntityManager = (HibernateEntityManager)entityManager;
Session session = null;
try {
   session = hibernateEntityManager.getSession();
   SessionFactory sessionFactory = session.getSessionFactory();
   sessionFactory.evict(ApplicationLog.class);
} finally {
   session.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Look for getDelegate() in jpa/annotation docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 1:23 pm 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
Yeah, but this method does not help some much. I still have to get the sessionFactory using a Session instance...

New code:
Code:
@PersistenceContext
protected EntityManager entityManager;
...
Session session = (Session)entityManager.getDelegator();
try {
   SessionFactory sessionFactory = session.getSessionFactory();
   sessionFactory.evict(ApplicationLog.class);
} finally {
   session.close();
}

Do you have other idea? Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ask in jpa forum - i can't remember the supported method.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 1:31 pm 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
Unfortunately I've already checked the JPA api and there is no other method.

As I said before it would be useful if the interface HibernateEntityManager would have some getSessionFactory method...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 1:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
*ask in the JPA forum*

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 24, 2007 11:21 am 
Beginner
Beginner

Joined: Sun Sep 30, 2007 2:58 pm
Posts: 26
A better solution!

"( (HibernateEntityManagerFactory) emf).getSessionFactory()
and then the evict method you want
_________________
Emmanuel"

Thanks Emmanuel!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.