该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。! W- P: @6 l; x
1 Q' }5 f% v# r7 [0 k
如下面代码:- Session session = factory.openSession();
6 t5 |8 b: P6 U; C - try {
' o, G) w+ C0 E - Book book1 = (Book) session.get(Book.class, id);6 G/ q$ s) ~& f* P/ v8 v
- Book book2 = (Book) session.get(Book.class, id);
. A/ I4 P, r- A* m+ i& c7 O - } finally {+ P# d: {8 |5 Q
- session.close();
- p0 S& G: I' T: g ]" X: ~ - }
复制代码
' [% q3 r) g/ q+ q7 A' Y% X6 H执行该代码,只会显示一条sql语句。; |8 C- n6 z; y9 {
4 ]8 v+ F$ P j7 j# n
9 H+ {( s( D, l, | [$ {如果执行下面代码:- Session sess=sf.openSession();
$ z( [9 S7 {) J. O3 q - Book b=(Book) sess.get(Book.class, "PBN123");
( e3 u( Q3 O9 N6 o0 h3 L" O- f! E - assertEquals("spring Recipes", b.getName());/ u) c8 F; V5 y- h& i6 R9 x& j
- sess.close();
9 f0 l/ _) t0 C% H) I+ a - Session sess1=sf.openSession();* ]5 s- w6 c/ M, T; W. u
- Book b1=(Book) sess1.get(Book.class, "PBN123");
, a6 d. j7 {1 s# k - assertEquals("Spring Recipes", b1.getName());
# f- J( a! g4 b6 }! |7 y - sess1.close();
复制代码 就会显示2条SQL语句6 s& D1 X5 _" `; V Q. H0 A- i3 i
- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a
0 _1 s. @) U: ^9 z1 [ - s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
+ a, |# V j$ \0 s% b - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
* d* ^0 {& X- `- X0 g$ o - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher* }, a" ~( C$ `+ A4 T
- 1_.CODE where book0_.ISBN=?
- h( X( [$ }. S$ c - Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a
0 [3 y3 E/ }+ L$ \, [9 y8 u - s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
9 x# l! w5 S. n* ]1 u - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
" z7 Q% E9 I5 D: p$ e; R# e - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher
- E) P, M4 \, @% H* G3 T8 @3 m8 q$ V - 1_.CODE where book0_.ISBN=?
复制代码 + Z: j3 [, r6 V# _+ \
7 ?8 p- C7 x& p
& u; ?7 ]* T$ h
; r6 N, i! J9 K+ c# l |
|