Last active
May 19, 2016 20:12
-
-
Save lynas/558e139e37b7446ffb417b2d35933bca to your computer and use it in GitHub Desktop.
Hibernate second level cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Hibernate second level cache | |
Dependency | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-ehcache</artifactId> | |
<version>5.1.0.Final</version> | |
</dependency> | |
@Bean(name = "sessionFactory") | |
public LocalSessionFactoryBean hibernate5SessionFactoryBean() { | |
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); | |
localSessionFactoryBean.setDataSource((DataSource) appContext.getBean("DataSource")); | |
localSessionFactoryBean.setAnnotatedClasses( | |
AppUser.class | |
); | |
Properties properties = new Properties(); | |
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); | |
//properties.put("hibernate.current_session_context_class","thread"); | |
properties.put("hibernate.hbm2ddl.auto", "update"); | |
properties.put("hibernate.use_second_level_cache", true); | |
properties.put("hibernate.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"); | |
properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"); | |
localSessionFactoryBean.setHibernateProperties(properties); | |
return localSessionFactoryBean; | |
} | |
@Entity | |
@Cacheable | |
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY) | |
public class Account implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column(nullable = false) | |
private String name; | |
@ManyToOne | |
private Book book; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment