1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class Client
{ public static void main(String[] args)
{ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = null ;
try { /*开启一个事务*/ tx = session.beginTransaction(); /*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/ Customer customer1 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002"); System.out.println("customer.getUsername is"+customer1.getUsername()); /*事务提交*/ tx.commit(); System.out.println("-------------------------------------"); /*开启一个新事务*/ tx = session.beginTransaction(); /*从数据库中获取id="402881e534fa5a440134fa5a45340002"的Customer对象*/ Customer customer2 = (Customer)session.get(Customer.class, "402881e534fa5a440134fa5a45340002"); System.out.println("customer2.getUsername is"+customer2.getUsername()); /*事务提交*/ tx.commit(); System.out.println("-------------------------------------"); /*比较两个get()方法获取的对象是否是同一个对象*/ System.out.println( "customer1 == customer2 result is " +(customer1==customer2));
} catch (Exception e)
{ if (tx!= null )
{ tx.rollback(); } } finally { session.close(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Test public void testCache2() {
Session session1 = sf.openSession(); //获得Session1
session1.beginTransaction(); Category c = (Category)session1.load(Category. class , 1 );
System.out.println(c.getName()); session1.getTransaction().commit(); session1.close(); Session session2 = sf.openSession(); //获得Session2
session2.beginTransaction(); Category c2 = (Category)session2.load(Category. class , 1 );
System.out.println(c2.getName()); session2.getTransaction().commit(); session2.close(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
< ehcache >
<!-- 缓存到硬盘的路径 --> < diskStore path = "d:/ehcache" />
< defaultCache
maxElementsInMemory = "200" <!-- 最多缓存多少个对象 -->
eternal="false" <!-- 内存中的对象是否永远不变 -->
timeToIdleSeconds="50" <!--发呆了多长时间,没有人访问它,这么长时间清除 -->
timeToLiveSeconds="60" <!--活了多长时间,活了1200秒后就可以拿走,一般Live要比Idle设置的时间长 -->
overflowToDisk="true" <!--内存中溢出就放到硬盘上 -->
/> <!-- 指定缓存的对象,缓存哪一个实体类 下面出现的的属性覆盖上面出现的,没出现的继承上面的。 --> < cache name = "com.suxiaolei.hibernate.pojos.Order"
maxElementsInMemory = "200"
eternal = "true"
timeToIdleSeconds = "0"
timeToLiveSeconds = "0"
overflowToDisk = "false"
/> </ ehcache >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< hibernate-mapping >
< class name = "com.suxiaolei.hibernate.pojos.Order" table = "orders" >
< cache usage = "read-only" />
< id name = "id" type = "string" >
< column name = "id" ></ column >
< generator class = "uuid" ></ generator >
</ id >
< property name = "orderNumber" column = "orderNumber" type = "string" ></ property >
< property name = "cost" column = "cost" type = "integer" ></ property >
< many-to-one name = "customer" class = "com.suxiaolei.hibernate.pojos.Customer"
column = "customer_id" cascade = "save-update" >
</ many-to-one >
</ class >
</ hibernate-mapping >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< hibernate-mapping >
< class name = "com.suxiaolei.hibernate.pojos.Customer" table = "customer" >
<!-- 主键设置--> < id name = "id" type = "string" >
< column name = "id" ></ column >
< generator class = "uuid" ></ generator >
</ id >
<!-- 属性设置--> < property name = "username" column = "username" type = "string" ></ property >
< property name = "balance" column = "balance" type = "integer" ></ property >
< set name = "orders" inverse = "true" cascade = "all" lazy = "false" fetch = "join" >
< cache usage = "read-only" />
< key column = "customer_id" ></ key >
< one-to-many class = "com.suxiaolei.hibernate.pojos.Order" />
</ set >
</ class >
</ hibernate-mapping >
|
refurl:http://flyingsnail.blog.51cto.com/5341669/1282367
相关推荐
hibernate一级缓存和二级缓存的区别与联系
Hibernate 一级缓存和二级缓存的区别
Hibernate二级缓存Hibernate二级缓存Hibernate二级缓存Hibernate二级缓存Hibernate二级缓存
hibernate的一级缓存和二级缓存,hibernate的一级缓存和二级缓存,hibernate的一级缓存和二级缓存,hibernate的一级缓存和二级缓存,hibernate的一级缓存和二级缓存
hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解hibernate 二级缓存详解
Hibernate一级缓存、二级缓存以及查询缓存实例
传播智客,为Spring集成的Hibernate配置二级缓存、cache.xml
主要讲解hibernate缓存的配置和和使用方法,以及一级缓存和二级缓存的定义和区别,和查询缓存的使用
基于hibernate二级缓存使用的源码实例
Hibernate 二级缓存
为Spring集成的Hibernate配置二级缓存
NULL 博文链接:https://cdxs2.iteye.com/blog/1936594
用以介绍hibernate 框架的缓存机制
hibernate二级缓存 ehcache
hibernate二级缓存介绍及演示代码
hibernate5.1二级缓存包
呈现了hibernate二级缓存 的基本用法 相信可参照相关文档 来学习
NULL 博文链接:https://12345678.iteye.com/blog/721910
基于springboot最新版本2.1.4.RELEASE,采用jpa为dao层框架,配置redis(redisson)为hibernate的二级缓存。
NULL 博文链接:https://364232252.iteye.com/blog/2369161