该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。0 ?- f: U4 N1 L) \5 g
7 _! x- f3 x! l& S. T" C3 U3 T
如下面代码:- Session session = factory.openSession();
2 _- H. r: T$ h y& p! e6 e - try {
' R( U0 d0 q( g" v - Book book1 = (Book) session.get(Book.class, id);
2 C; ^ X6 n% O. f - Book book2 = (Book) session.get(Book.class, id);; t1 R0 U5 a* H; Q% Q8 z/ k
- } finally {
/ t5 S8 O. W8 D( B - session.close();
, S" a9 j5 N, K - }
复制代码 6 W' J% U- }: ~# [
执行该代码,只会显示一条sql语句。
* @! t, O# C5 T$ B) T# Y# P# S( K( ^( [. N5 z
! B* e8 J" b, C/ L3 F1 V
如果执行下面代码:- Session sess=sf.openSession();
- }2 N, y8 E$ l - Book b=(Book) sess.get(Book.class, "PBN123");7 g$ R9 m$ e( c5 ?0 H
- assertEquals("spring Recipes", b.getName());) h( d+ O! w* a. ? u2 h3 Q1 ]; T
- sess.close();( r% @3 [6 t/ e
- Session sess1=sf.openSession();
# {4 L7 k3 \; Q4 h# I3 M - Book b1=(Book) sess1.get(Book.class, "PBN123");
4 x' S! a; w9 q/ g - assertEquals("Spring Recipes", b1.getName());/ d( e/ I: U; ]$ `% G0 K: ]
- sess1.close();
复制代码 就会显示2条SQL语句
9 J; E" J) z- ?) y- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a+ \5 }, G, Z: h
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi; K, L; }# l, b, j" s8 Q# q
- sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
) k. t3 V( l8 N/ R; o" {$ q- U* v - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher
( H: D# M! X, x- M& x3 |" u, F4 N4 A - 1_.CODE where book0_.ISBN=?
2 [2 K7 {4 R* g% O2 C1 q: g) e - Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a
c7 v) [- w: p& W% I - s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
9 \3 j' V8 s( @ - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_18 T( ^+ A4 @) J- A# b; d1 ~
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher
) F5 C5 M S4 T& D7 K - 1_.CODE where book0_.ISBN=?
复制代码
/ I+ Z# j. h" s# H/ S9 c7 y: J" J8 K" f0 w- p0 p( n
2 T0 e% ?/ G! m" a) F- q
$ h& N8 f$ y8 j! c; n* ?, [6 e |
|