Hibernate的一二级缓存
来源:互联网 发布:dota2数据分析 编辑:程序博客网 时间:2024/05/21 17:23
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
>
0 0
- Hibernate的一二级缓存:二级缓存
- Hibernate的一二级缓存
- 【Hibernate】Hibernate的一二级缓存
- Hibernate的一二级缓存区别
- hibernate一、二级缓存的实现
- hibernate的二级缓存(一)
- Hibernate的一二级缓存:一级缓存
- Hibernate 一二级缓存的使用场景
- Hibernate的一,二级缓存和lazy简介
- Hibernate一、二级缓存原理
- Hibernate一二级缓存
- Hibernate二级缓存(一)
- Hibernate(六)一二级缓存
- hibernate一二级缓存
- hibernate二级缓存,一二级缓存交互,CacheMode
- hibernate一二级缓存(一)
- hibernate一二级缓存(二)
- hibernate进阶--一/二级缓存策略
- 如何在Linux上格式化并创建分区
- 图解Ubuntu Server 14.04安装(详细-一个步骤一图配图为12.04)
- 最重要,最常用和有用的Linux命令大集合
- js获取地址栏参数的方法
- UITabbarbutton 页面之间的跳转
- Hibernate的一二级缓存
- Android Studio你不知道的调试技巧
- C#使用多线程进行并发处理
- HTTP笔记(六)状态码
- C#验证码识别类完整实例
- ios breakpoint1.2bug
- mac shell 初学 变量输入输出
- 实习之多线程
- RSA