该用户从未签到
|
一级缓存是在事务级别或工作单元级别。hibernate默认启用一级缓存,一级缓存与session关联,如果相同的query在相同的session中执行多次,那么与该query关联的data就会缓存,一级缓存的scope是session。8 i n' V1 u. E# q- L; }
1 M( W+ m( ]# V8 N4 _: E/ X5 Q如下面代码:- Session session = factory.openSession();
/ ~5 f8 V7 f! F - try {3 z5 u; G+ r8 @6 ~4 X
- Book book1 = (Book) session.get(Book.class, id);8 d+ c E; J; W1 V7 |
- Book book2 = (Book) session.get(Book.class, id);
* i8 u/ e7 P2 v8 H0 U4 J - } finally {; `& h6 S* d. T, ^! o8 o) z* ^' W4 V- W
- session.close();7 P! T$ {- ~6 W0 c
- }
复制代码 ! M* U% H$ _" l6 r9 h
执行该代码,只会显示一条sql语句。2 l9 G" N4 f" U- x+ l; C9 ?
4 y6 p% `3 y" Z/ b; V+ W2 G" l4 g6 P) K& B. A
如果执行下面代码:- Session sess=sf.openSession();
; g3 M" M. e' M' O# k/ R - Book b=(Book) sess.get(Book.class, "PBN123");% k8 S& ^( u) Y# w+ w7 f* o0 f
- assertEquals("spring Recipes", b.getName());5 H% M* r9 l1 p1 \7 ^; \$ G, }* Y
- sess.close();& m# C" S2 x1 [5 h% h& r
- Session sess1=sf.openSession();
+ _2 f( W. R6 a5 } - Book b1=(Book) sess1.get(Book.class, "PBN123");' V6 M" g7 l& D1 p" D: j
- assertEquals("Spring Recipes", b1.getName());
! Q) _% M- O" R- a0 B. A- l - sess1.close();
复制代码 就会显示2条SQL语句, f+ q' h% R* F2 G" ?! y
- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a7 X4 t' s' P, T' o
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi; J+ s# |/ z: n1 }
- sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_1
5 C) E% |1 T% O3 q6 I/ m) ]; v+ m2 a - _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher7 _( p3 ^- h* c3 G K
- 1_.CODE where book0_.ISBN=?8 C# P2 x' Z: E( @" o9 z
- Hibernate: select book0_.ISBN as ISBN1_0_0_, book0_.NAME as NAME2_0_0_, book0_.PUBLISHDATE a( u0 h- ]7 v( p8 Z( K& `
- s PUBLISHD3_0_0_, book0_.PRICE as PRICE4_0_0_, book0_.PUBLISHERCODE as PUBLISHE5_0_0_, publi
8 v2 U4 A2 D9 H6 Z - sher1_.CODE as CODE1_1_1_, publisher1_.NAME as NAME2_1_1_, publisher1_.ADDRESS as ADDRESS3_14 B* G9 y; h/ \6 c/ v2 F+ W/ c
- _1_ from BOOK book0_ left outer join PUBLISHER publisher1_ on book0_.PUBLISHERCODE=publisher/ I8 r) ?3 ^6 |3 w j& @+ M0 Z& s5 J
- 1_.CODE where book0_.ISBN=?
复制代码 4 M8 K8 H% s7 M! j/ o
& M9 _, [' V* j) {" D1 j; v
% s2 B# l& k% o( m: g
1 c9 |4 [3 T( e: J9 ^- {! p: m4 ] |
|