TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
使用复合主键的实体类必须实现Serializable接口。
, S# l( L5 Y* D 2. 使用复合主键的实体类必须重写equals和hashCode方法。: G! L; t7 s- v
/*实体类,使用复合主键必须实现Serializable接口*/
7 L2 ~$ U( i) n. _- public class People implements Serializable
! p- m3 g5 q! A, K! U. g - {
0 Y! a/ f5 y" d - private static final long serialVersionUID = -4888836126783955019L;
7 v7 w6 q3 ?- W* k& D -
2 h* ~; N5 h( e* F0 d - private String id;
- }) d+ J' n0 ~# M; G - private String name;) P1 H3 z& K! l( }7 L
- private int age;! N5 R8 A" ]8 Q5 C
- e' y; A f; P) G# e
- public People()
. n! D# t0 H3 X7 J2 ~ - {
6 q- l, Q! B% n# X7 v -
# E/ A2 k; V5 B# g: T - }, n* M) G3 R( V* ?3 ?1 ?
- % \8 T- C. ? r5 z
- public String getId()
& g& y3 J' g8 |3 E - {
# k8 f0 R% e/ x - return id;0 u; C6 l- R, g: M
- }
9 }: n* K* } r( t3 V! m$ }- @
) k( T" l8 G6 k& W! V- public void setId(String id)% i1 g% U0 l5 P4 ^, P
- {% ^; m0 l( @! w9 J, g5 n9 c
- this.id = id;
9 D: {! _5 n$ q- B6 z I! G - }
/ ]0 E) e. m l/ {6 ? - # F0 {/ |9 Z: G' M
- public String getName()) v6 H0 I3 t: y' x! { i
- {
' R% C) S) v3 b" q i; G5 ?0 A - return name;, R& F. a* \; d( w+ o* T2 b$ B B
- }
' i5 C% V% r. Z c# w3 E X# I4 R - 9 g' n! c* d, m8 V+ f& A" F m
- public void setName(String name)
) i- Y6 G$ J% g7 V2 P+ h/ Y5 C - { I& a% A/ N, [( a) b* X; ?; E" w8 k
- this.name = name;
7 w- B3 ]& l7 G) h6 j; t- N - }' p, \6 D4 V A5 p6 A: T9 {8 Z
- 7 b. y1 s l. j
- public int getAge()* ^7 Q6 O3 V* V, q
- {
4 ^$ Q3 Z; |! e& o% r. L - return age;
- a. J/ b5 p( Q - }
, p7 q+ T1 a S+ i- y' B* P - & K! O9 j( n) O! r
- public void setAge(int age)
% [/ m8 w* O: f) |0 J" d8 y - {
5 V$ E. d' D1 a1 Z# w; @: K - this.age = age;2 ~$ t3 _5 B/ {5 U" k$ z0 }
- }2 G2 M j# P) X! p5 i7 p
- 1 q. A3 `& h% p X' Z
- @Override
% z2 T" i6 O) A- Q6 a - public int hashCode()( o {7 J5 \% \9 M5 E
- {
. l- E# @0 S w4 {0 i4 a" o! Z - final int prime = 31;
M% G+ j e( \& r0 w% D* E - int result = 1;
6 y) P% b8 o5 Z - result = prime * result + ((id == null) ? 0 : id.hashCode());4 C1 e% x$ U S$ P/ n$ P G% T" G- j
- result = prime * result + ((name == null) ? 0 : name.hashCode());
! S4 ?2 W7 y( l: x - return result;9 c- R4 f7 R8 U5 N3 A
- }
% p6 V7 Y/ M. P
% [" I) q% C' U3 V' O- @Override
2 d: F, |$ b" u( B - public boolean equals(Object obj)
: U- R, m v% t3 i- X - {
5 f; B" _! G: ~8 F! p# M- I - if (this == obj)
* p M, _0 @% V. L: e- P4 J7 |* i - return true;" x1 [$ q8 K4 ]) D8 O+ z; o7 V3 ^4 S
- if (obj == null); g" } V) p1 P8 ?& h- ]- ?
- return false;
1 U0 E5 w& M3 h( g0 j8 d - if (getClass() != obj.getClass())* C, `1 x z8 {! B" g
- return false;0 d$ @2 V: s9 ~' P+ Y w g' S' m7 I
- People other = (People) obj;
: E7 H ?* N- o5 r$ u - if (id == null)
! J$ R: r; a! E% A7 { - {( U& Q7 _: h# T/ z
- if (other.id != null)5 @9 q$ h+ f5 b' @3 h- x2 g! K
- return false;
) E) q' V* f( a - }
8 D: k3 o- l, A3 ]* k! {# g - else if (!id.equals(other.id))
" y8 U5 X. w3 ~; ` - return false;: Q* B3 |% B2 r4 W# `
- if (name == null)7 g0 [8 f" _; D t, \3 M
- {
+ B2 Q3 r- [/ I3 f$ v) `+ d - if (other.name != null)
. J" n/ u: S. o. B' [! A2 N l E - return false;
z/ B. e" M9 i& h. u& U2 M! @ - }
3 I) {) i6 I3 V2 T* _4 j+ T0 s - else if (!name.equals(other.name))+ J+ b1 b* q& F T! Q/ n# G/ j A
- return false;* P1 Y8 H1 K7 A) n- ?+ d
- return true;! `0 T2 i' E+ g) Y# s; \
- }# l( Y. k# s3 V( t* r. m' G
- }
复制代码 6 W5 ^' u( ~& s* c" }9 e/ O3 D- j; X
" \) f9 Y" P, B/ x+ p, Y' c8 x* g _) i! \
& k m; t9 P' B# \ |
|