该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。
- I; X; X, y1 E
( R) N' g! @" a! R. h4 m如下面代码:- Session session = factory.openSession();
, E# x, q, `8 Q9 `9 Y - try {
# L" l+ H$ u/ F. R0 m- b" C - Book book1 = (Book) session.get(Book.class, id);4 y8 T4 y- E% D/ K* l5 ?$ r& S+ A
- Book book2 = (Book) session.get(Book.class, id);
2 B2 }0 @5 d I6 S' y/ K. U - } finally {- l6 k3 b/ \+ e" W$ A( ^9 X, k$ |
- session.close();
' z7 C1 J1 }7 {; c' ?; x$ k - }
复制代码
$ K/ a& W: c3 m' N执行该代码,只会显示一条sql语句。
# K" Q) o5 F1 r# ]
0 X2 V* U) O; [/ D3 K+ |. w+ `9 n" a8 z* l. i
如果执行下面代码:- Session sess=sf.openSession();
( ~- I- F6 F4 u. C5 S \ - Book b=(Book) sess.get(Book.class, "PBN123");
$ v) E) _& ?; A. u8 v! @ - assertEquals("spring Recipes", b.getName());
( l1 M7 {$ P$ Q5 ^* _; p - sess.close();
& }5 `8 j0 H. d$ S - Session sess1=sf.openSession();4 K# M+ A' ~3 i/ a* X2 u7 c& K
- Book b1=(Book) sess1.get(Book.class, "PBN123");
1 g5 X5 q& v9 j - assertEquals("Spring Recipes", b1.getName());
. d4 H6 U0 O& T$ \7 q - sess1.close();
复制代码 就会显示2条SQL语句
2 l# T! u% B7 D' y& K- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a' y. _) ]4 Q9 q" f, x# e
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
8 h. F; |2 A/ {2 J - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1! {0 S/ b! u$ P1 z5 c( Y
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher: O/ E$ q% V' W) o+ y _, |' f, u
- 1_.CODE where book0_.ISBN=?
6 e ?; E9 B* ~ - Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a
+ L. {# u3 g4 a3 ~" v4 V - s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
7 N: H4 ~5 Q1 K - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
/ ] p" K4 D/ B' W9 F. N2 p8 ] - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher% K. i; x+ i- X; a! Z
- 1_.CODE where book0_.ISBN=?
复制代码
5 l; Q" ^ U- I$ K! R* g8 ]+ L, U0 ?7 B! T! f3 Y
0 j8 a! n+ `+ q! G7 f
: [$ j1 W7 z: {3 D9 q* {6 N! P |
|