Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Cache annotations

Why in the hibernate docs do I see references with both annotations on entities for caching?

eg: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#performance-cache-mapping

@Entity 
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Forest { ... }

Is both the @Cacheable and the @Cache annotations required?

like image 797
Seamus McMorrow Avatar asked Sep 11 '15 20:09

Seamus McMorrow


2 Answers

if you are only using Hibernate, using only @Cache will do the trick, since @Cacheable is an another option you could use along with JPA or Spring.

@Cacheable is usually used for JPA Entities. In the docs, they are just referring to the @Cache annotation, so just focus on that.

Here is a good resource to look at for Hibernate Cache Levels: http://www.javacodegeeks.com/2012/02/hibernate-cache-levels-tutorial.html

Hope this helps. Tried my best :)

like image 173
parthpar Avatar answered Nov 09 '22 07:11

parthpar


In short: no.

In long: @Cache is the cache interface provided by Hibernate, while @Cacheable is the interface provided by JPA specification. You can choose between one of them, and even use both. Note that @Cacheable will work only if your persistence.xml has caching element set to ENABLE_SELECTIVE or DISABLE_SELECTIVE.

like image 31
Bonifacio Avatar answered Nov 09 '22 07:11

Bonifacio