hibernate二级跟查询缓存使用
hibernate二级和查询缓存使用
二级缓存
一。先导入解压后hibernate-release-4.2.8.Final\lib\optional\ehcache目录下的jar包
ehcache-core-2.4.3.jar
hibernate-ehcache-4.2.8.Final.jar
slf4j-api-1.6.1.jar
二。配置hibernate.cfg.xml
三。拷贝下载包\hibernate-release-4.2.8.Final\project\hibernate-ehcache\bin项目目录下的ehcache.xml文件至项目根目录下
四。在实体类上加上@Cache注释
五,测试
测试结果,只发出一条语句,输出两次结果
查询缓存
依赖二级缓存
一。配置hibernate.cfg.xml,打开查询缓存
二。测试,查询时注意加setCacheable(true)
测试结果:只发出一条sql语句
二级缓存
一。先导入解压后hibernate-release-4.2.8.Final\lib\optional\ehcache目录下的jar包
ehcache-core-2.4.3.jar
hibernate-ehcache-4.2.8.Final.jar
slf4j-api-1.6.1.jar
二。配置hibernate.cfg.xml
<property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
三。拷贝下载包\hibernate-release-4.2.8.Final\project\hibernate-ehcache\bin项目目录下的ehcache.xml文件至项目根目录下
四。在实体类上加上@Cache注释
@Entity @Cache(usage=CacheConcurrencyStrategy.READ_WRITE) public class Student{ private int id; private String name; private Set<Teacher> students =new HashSet<Teacher>(); @Id @GeneratedValue public int getId() { return id; } @ManyToMany(mappedBy="students") public Set<Teacher> getStudents() { return students; } public void setStudents(Set<Teacher> students) { this.students = students; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } }
五,测试
public class OneToOneORMappingTest { public static void main(String[] args) { Configuration cfg = new AnnotationConfiguration().configure("/hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); Student student =(Student) session.load(Student.class, 0); System.out.println(student.getName()); session.getTransaction().commit(); session.close(); Session session2 = sf.openSession(); session2.beginTransaction(); //查询 Student student2 =(Student) session2.load(Student.class,0); System.out.println(student2.getName()); session2.getTransaction().commit(); session2.close(); sf.close(); } }
测试结果,只发出一条语句,输出两次结果
Hibernate: select student0_.id as id1_0_0_, student0_.name as name2_0_0_ from Student student0_ where student0_.id=? zhangsan zhangsan
查询缓存
依赖二级缓存
一。配置hibernate.cfg.xml,打开查询缓存
<property name="hibernate.cache.use_query_cache">true</property>
二。测试,查询时注意加setCacheable(true)
public class OneToOneORMappingTest { public static void main(String[] args) { Configuration cfg = new AnnotationConfiguration().configure("/hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); //查询 List<Student> result = session.createQuery("from Student").setCacheable(true).list(); for(Student s :result){ System.out.println(s.getName()); } session.getTransaction().commit(); session.close(); Session session2 = sf.openSession(); session2.beginTransaction(); //查询 List result2 = session2.createQuery("from Student").setCacheable(true).list(); session2.getTransaction().commit(); session2.close(); sf.close(); } }
测试结果:只发出一条sql语句