该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。+ I: L+ m2 x2 ]( C8 t7 a; v
9 W8 ^' c5 a: B$ j/ }/ R7 q如下面代码:- Session session = factory.openSession();
9 N* D2 w1 Z K _ X - try {$ v* W- c1 i3 R2 ^# l5 k& U6 q
- Book book1 = (Book) session.get(Book.class, id);
5 f* \5 h& b7 X - Book book2 = (Book) session.get(Book.class, id);4 |* o1 H: S) f3 o0 V8 C
- } finally {
. u5 A9 y4 e2 k. Q/ w3 c' f2 ~ - session.close();. ^8 J: w- t% G- h3 ?3 X
- }
复制代码 # _, k( ?9 u3 U) F; \
执行该代码,只会显示一条sql语句。
/ X/ F; z0 X$ W' D; ` {$ H
; _, Q# K* w$ ?4 X, j* N0 t4 ]* D q! J* ?3 f" b0 |: \
如果执行下面代码:- Session sess=sf.openSession();
2 A% q9 x* l' ]: n5 l# ^ - Book b=(Book) sess.get(Book.class, "PBN123"); c+ p6 \" X4 H, u
- assertEquals("spring Recipes", b.getName());/ S) B% P: a" ]6 b0 q# ~( }
- sess.close();
5 Y$ b/ b, S! [4 S. E. r1 @, B7 y - Session sess1=sf.openSession();
3 n) y# }: }" L7 F+ e2 Q# l* b - Book b1=(Book) sess1.get(Book.class, "PBN123");6 _, f' }. t' _
- assertEquals("Spring Recipes", b1.getName());
& X! @# |& t2 E% ]9 g3 Z1 {; p - sess1.close();
复制代码 就会显示2条SQL语句1 X) H) P' r7 A! M/ U! @# c% c
- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a7 g% K: X1 R4 G5 D7 [0 F+ y
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi6 @. u, c3 P9 H$ k& b: s* | K
- sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1" K. h5 ]3 h4 R7 ~
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher q7 w2 l7 Y S0 u0 a
- 1_.CODE where book0_.ISBN=?. a% j- h1 ^; `/ @- \5 p
- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a& k5 V& S5 s) O$ K% q# E3 Z
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi( ^4 J+ I8 b& y) H6 I
- sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_14 j5 e) ?% ~3 A, Z
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher% I7 H$ P" ?7 I, l
- 1_.CODE where book0_.ISBN=?
复制代码 0 Z9 F$ p! [" S5 Y: W0 ?$ Y2 z; ]
/ ]% M6 d Y. j. J
; }- l8 a% G. s# l# r8 ^
; v, h4 p$ C7 d: v. ], a' M" V |
|