Caching entities
Unless you have set shared-cache-mode
to ALL
, Hibernate will not automatically cache your entities. You have to select which entities or queries need to be cached. This is definitely the safest option since indiscriminate caching can hurt performance. The following example shows how to do this for JPA entities using annotations:
import javax.persistence.*; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @Entity @Cacheable @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL, region ="properties") public class Property { @Id @Column(name="key") private String key; @Column(name="value") private String value; // Getter & setters omitted for brevity }
Using JPA annotations
The @javax.persistence.Cacheable
annotation dictates whether this entity class should be cached in the second-level cache. This is only applicable when the shared-cache-mode
is not set to ALL
.
Using Hibernate annotations
The @org.hibernate.annotations...