Hibernate Second Level Cache
Directory Structure
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SecondLevelCache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SecondLevelCache</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.8.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.6.8.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</project>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/new</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
</session-factory>
</hibernate-configuration>
Employee
package com.test.entity;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer eid;
private String ename;
private String dept;
}
HibernateUtil
package com.test.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.test.entity.Employee;
public class HibernateUtil {
static SessionFactory factory;
static {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
cfg.addAnnotatedClass(Employee.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
App
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Employee;
import com.test.util.HibernateUtil;
public class App
{
public static void main( String[] args )
{
//get sessionFactory
SessionFactory factory = HibernateUtil.getSessionFactory();
//get Session
Session ses = HibernateUtil.getSession();
try(factory;ses){
//create emp obj
Employee emp = new Employee();
emp.setEname("sam");
emp.setDept("QA");
//begin the transaction
ses.beginTransaction();
//save the obj
ses.save(emp);
//commit
ses.getTransaction().commit();
}
}
}
Test
package com.test;
import org.hibernate.Cache;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.test.entity.Employee;
import com.test.util.HibernateUtil;
public class Test {
public static void main(String[] args) {
//get sessionFactory
SessionFactory factory = HibernateUtil.getSessionFactory();
//get Session
Session ses = HibernateUtil.getSession();
try(factory;ses){
//get the emp obj
Employee emp = ses.get(Employee.class, 1);//l1 and l2 cache
System.out.println(emp);
Employee emp1 = ses.get(Employee.class, 1);//l1
System.out.println(emp);
ses.evict(emp1);//remove emp from l1
Employee emp2 = ses.get(Employee.class, 1);//chech l1 , see l2 copy l1
System.out.println(emp);
ses.clear();
Cache cache = factory.getCache();
cache.evictAll();
Employee emp3 = ses.get(Employee.class, 1);//chech l1 , see l2 copy l1
System.out.println(emp);
}
}
}
Comments
Post a Comment