该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。4 D& c( Z& {# K. E" J) q* @- [! Z
0 c3 L" e7 }9 [; ]0 a; _8 {
如下面代码:- Session session = factory.openSession();0 g3 i" m; L0 e8 [0 k
- try {
* Q# f' B9 [; f: Y6 v - Book book1 = (Book) session.get(Book.class, id);
+ Y+ k4 ?7 {* e! G2 y - Book book2 = (Book) session.get(Book.class, id);$ J; W3 r* R4 [* D6 \; S
- } finally {
, V4 V0 ]* ], {! a7 [ l - session.close();
5 l1 F# y1 J4 w8 ~& H8 J' B) T - }
复制代码
9 N1 E- Z% p3 E; P执行该代码,只会显示一条sql语句。
1 p" G4 c: O0 ]8 S& h/ R7 p* F
/ d2 o+ |& h; [6 Z- c+ y9 c
$ w# [8 A( j; z4 f2 C& e$ B如果执行下面代码:- Session sess=sf.openSession();
6 J. V N* }9 w9 g$ ?& R - Book b=(Book) sess.get(Book.class, "PBN123");/ A% _% a$ q V
- assertEquals("spring Recipes", b.getName());- C6 O0 }1 v M2 x: e) b
- sess.close();
* ~7 ~9 r4 W3 H9 x% S, W, D - Session sess1=sf.openSession();
3 g9 N2 M9 K3 A6 M* N' j0 N - Book b1=(Book) sess1.get(Book.class, "PBN123");
$ D. j3 }5 Q3 c% h1 \) E, Q8 i) m0 @ - assertEquals("Spring Recipes", b1.getName());) `' @7 f# Q! V+ }" b- I
- sess1.close();
复制代码 就会显示2条SQL语句
" b G- A. ~+ _3 s- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a' w x& S9 j5 [1 a! K% B( }
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi9 T1 R" F- V# N: {
- sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1# w9 V e/ Y; j6 k
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher
5 f" c" L# S$ Z) v; o - 1_.CODE where book0_.ISBN=?
0 ^5 t$ P8 R: p) Z) [0 Q; E% ?( x7 p - Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a( _5 ]+ Y' K. f! v% T
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
4 p9 A7 l: p9 ?0 `/ f8 k" m - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
0 `: W! h5 M( H4 ^+ m - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher. s# C* @9 O& Y! I# [
- 1_.CODE where book0_.ISBN=?
复制代码
8 B0 U0 c5 E$ w3 v& N; W- w
/ [- P' @) f. Z9 m: }( y
9 g% `+ B# P: Z% m, J: b
) c# V3 o. @( o- V |
|