标签:span mit this throw service back 技术分享 begin 1对多
一、缓存的概念:
这里以Class —— Student,1对多的关系(单向)来例子说明下:
Class.java:
package com.cy.model; public class Class { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Student.java:
package com.cy.model; import com.cy.model.Class; public class Student { private int id; private String name; private Class c; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Class getC() { return c; } public void setC(Class c) { this.c = c; } }
Class.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Class" table="t_class"> <id name="id" column="classId"> <generator class="native"></generator> </id> <property name="name" column="className"></property> </class> </hibernate-mapping>
Student.hbm.xml:
<hibernate-mapping package="com.cy.model"> <class name="Student" table="t_student"> <id name="id" column="stuId"> <generator class="native"></generator> </id> <property name="name" column="stuName"></property> <many-to-one name="c" class="com.cy.model.Class" column="classId" cascade="save-update"></many-to-one> </class> </hibernate-mapping>
StudentTest.java:
package com.cy.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.cy.model.Class; import com.cy.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session=sessionFactory.openSession(); session.beginTransaction(); } @After public void tearDown() throws Exception { session.getTransaction().commit(); session.close(); } /** * 同一个session中,同一个事务是有一级缓存的; */ @Test public void testCache1(){ Class c = (Class) session.get(Class.class, 1); System.out.println(c.getName()); Class c2 = (Class) session.get(Class.class, 1); System.out.println(c2.getName()); System.out.println(c == c2); /* * Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? 08计算机本科 08计算机本科 true 第一次取c1的时候,已经将取出来的数据c1放到缓存中。 第二次取c2的时候,是有session缓存的。首选从缓存中找,找到,直接从缓存中拿,而不到数据库中去取了。 两个是同一个OID对象; */ } /** * 在不同的事务中,是否有缓存? */ @Test public void testCache2(){ Session session1 = sessionFactory.openSession(); session1.beginTransaction(); Class c = (Class) session1.get(Class.class, 1); System.out.println(c.getName()); session1.getTransaction().commit(); session1.close(); Session session2 = sessionFactory.openSession(); session2.beginTransaction(); Class c2 = (Class) session2.get(Class.class, 1); System.out.println(c2.getName()); session2.getTransaction().commit(); session2.close(); System.out.println(c == c2); /* * Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? 08计算机本科 Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? 08计算机本科 false 虽然在同一个session中有缓存,但是这里新开辟了一个事务,发出两个sql 不同的事务是不能共用一级缓存的。 一级缓存只适用于同一个session,同一个事务; */ } }
三、二级缓存策略提供商:
Hibernate学习11——配置Hibernate二级缓存
标签:span mit this throw service back 技术分享 begin 1对多
原文地址:http://www.cnblogs.com/tenWood/p/7259681.html