TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
使用复合主键的实体类必须实现Serializable接口。( q9 X9 I5 e; J7 w# u
2. 使用复合主键的实体类必须重写equals和hashCode方法。3 q5 u7 T4 @/ E0 x: c l2 o/ X! n
/*实体类,使用复合主键必须实现Serializable接口*/
4 }0 V5 Q( |3 }" `, ]- public class People implements Serializable
0 d+ {/ [! S1 v$ t - {
: y% ^: R: j& K+ L" ` - private static final long serialVersionUID = -4888836126783955019L;0 L; ?1 N) X: M# S/ L
-
" y3 d4 r) \% @1 K a2 Y - private String id;
1 d* k5 v1 u5 ]4 h1 { - private String name;
' ~8 v7 {2 S& f; T - private int age;1 \) ^! D, o0 x' ?( X& l
- % h0 k5 O* [( g) I
- public People()! H+ l5 p" W/ Q! D$ C* v
- {
7 X7 Y- w# p, {3 c0 y9 y5 F8 i -
$ p$ d6 \# x& X0 z- @( F/ P - }
4 V1 G0 C3 E7 F! {! `& ?) a
5 e; o8 E4 e9 B7 T- public String getId(). C) {: h" F, X
- {# i0 D6 R" \8 ?" \* \' u4 d
- return id;8 M& v- Q2 i( ^4 s( M
- }
+ W+ @% f4 n: o; n
4 ^3 e( T1 g0 c) t& v, l. _- public void setId(String id)2 f$ i* E* W$ F! Q" j
- {
' n0 }4 G% x+ o) h( l8 _ e! G - this.id = id;
8 b6 q5 `& x. p; `# O - }5 {3 T7 R# e% s1 g" m2 M8 y! h
- 0 t- h; d- w6 x1 | w& Y2 m
- public String getName()1 y0 Z0 I% U+ E) c( I( A2 [
- {( Y, ~' `7 ~( V/ P
- return name;: k: K! h+ u( D/ N5 d
- }
! M# e& P# V4 e" d" n' N, o8 d
P! T5 a/ F4 |4 D+ W5 L( G- public void setName(String name)
3 Z& g7 L t' O/ U. B - {
+ L: n( Y9 m4 w0 h - this.name = name;
/ E* I( _" o! b - }
* N3 D, n3 Z$ `3 ~ - 3 B5 F; j2 D) f/ B' k3 w$ Y, ~! Z( ^
- public int getAge()
Y; Y7 Y8 Y. g u4 j0 x5 F/ u- N - {
: i. I( k! e8 p/ N* y - return age;
+ |2 P" c- Z9 p/ L8 i - }" r' l3 \ y5 [# I$ m) M
- ?7 Q5 f h4 {0 `2 s
- public void setAge(int age)
% d" d2 q' i* N2 m$ H2 o- n1 L0 g - {* Q3 B( T% N B6 P
- this.age = age;3 k9 ^. p+ g, ]5 i4 Q5 U' u j6 N z$ A
- }
. E& t6 i: b! o. A: S* y3 W. }" L
) _% C$ ^6 ]; f8 Y6 M7 a' r) B% O- @Override& p4 h& u, [$ Y" C* \
- public int hashCode()
" H" a( ?6 H, |0 ]$ g8 F: ] - {
4 C4 E) l# h0 ]. y3 T5 j - final int prime = 31;/ ]- q8 t+ g \' N
- int result = 1;! z/ P( a( a, O& S
- result = prime * result + ((id == null) ? 0 : id.hashCode());
* L2 p0 w: l) ^; ~% d - result = prime * result + ((name == null) ? 0 : name.hashCode());
8 T3 O/ R) ]0 `( w5 {: T - return result;8 j) H: S0 C, E3 Z! {8 x- Q" [3 a
- }# n M! z. Y( R. H1 D, a4 d
% R+ j; [/ d N4 j" z3 h' }7 c8 _- @Override3 g' _" x4 [3 c. _4 h
- public boolean equals(Object obj)7 g: m. o: Y( F' I" I6 r: S
- {- E3 ?7 j6 L% o6 u7 D+ ^
- if (this == obj): o- \5 V, H- C, q# |: Z: j3 Q
- return true;. F. ~7 s, _) Q5 c2 X$ c' @
- if (obj == null)& E% ~; p5 Z5 r' I3 P
- return false;" r( u; Y: _+ T( Q, A
- if (getClass() != obj.getClass())! ]$ ?* X$ ~& D2 y+ L. I5 ^) u" o
- return false;
% d/ o' B0 h; H% v ^4 F - People other = (People) obj;( A/ ]7 U- w0 ]
- if (id == null)
: F/ v, ^. B! ] - {! n$ r) E. s- S- }
- if (other.id != null)
7 n6 h% d: G9 \) ]" D - return false;) t5 s( ]3 l) C5 w4 u+ m
- }
" i( ?8 L4 [4 \0 r - else if (!id.equals(other.id)). ~- S) H& F9 w1 W
- return false;/ p& u2 }( q N: A# R
- if (name == null)6 [" ?* N. \) s& X3 e$ L% r
- {
" E H1 |( [, C( Q* Z - if (other.name != null): Z( g) b) S/ W6 ~2 T% ]/ q& P
- return false; y3 Q; m$ v8 G6 @# w1 w& h
- }
, E( }6 y) v: g0 j( ^" W - else if (!name.equals(other.name)), n* _2 I$ N6 v2 S0 B# E1 l& A
- return false;
* O3 F/ W# h: G' V - return true;
! `: w2 q3 x* n( @( I - }( A$ v8 ^. I/ s3 `/ u
- }
复制代码 ( D. B- D. s [- G
Z- {& D" Y7 t9 P8 [5 `+ R
) F2 q# x; ?6 E k4 g) S. ^
$ S7 G% H1 p3 ], D3 e" L8 x8 m |
|