O'Reilly logo

JBoss AS 7 Configuration, Deployment, and Administration by Francesco Marchioni

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Caching entities

Unless you have set shared-cache-mode to ALL, Hibernate will not cache entity automatically. You have to select which entities or queries need to be cached. This is definitely the safest option since indiscriminate caching can actually 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
}

The @javax.persistence.Cacheable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required