TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
使用复合主键的实体类必须实现Serializable接口。
: @- I4 z# o! g5 G ? l 2. 使用复合主键的实体类必须重写equals和hashCode方法。
; g: ` o6 f4 E" k- p/*实体类,使用复合主键必须实现Serializable接口*// Q: p3 T3 `2 h7 m
- public class People implements Serializable
8 \! v* i2 ^# Y3 c4 ~# M: `+ ] - {
' f" C' `* @2 b; R% z$ Q: J5 C - private static final long serialVersionUID = -4888836126783955019L;
' l$ I( w9 g: K/ y( K2 | -
2 t5 \" j& t8 ~- x - private String id;" d9 n { k+ c( N# Q. m0 ]% i1 N
- private String name;7 S$ @/ u& \* E0 } z* H; c0 Z4 t
- private int age;
, q$ O# ?! P9 `( k3 a - ! X& X' J5 O: ? H: Z8 G
- public People()8 ?0 U# G; D4 R& t& H
- {4 }! s2 p! } R" k1 \' V& E
- & U8 }) }6 T# K) z {' R6 ~- ^
- }
. I0 X! U7 H: y: B& D& ^: ~ - 8 _7 q, e E) V$ w0 ^0 s& L
- public String getId()
7 f7 \, x2 T b7 k; G F - {
% e& `* `: C+ C+ ~+ J - return id;0 f) [* h7 Z0 O# @' H, K3 B
- }
! p# L* {! ~) k0 Z- u- P - p# {& u" r. r. V D
- public void setId(String id)( n$ j/ B4 t5 d' p E; s, U
- {
h- k: ]# N3 f1 w, I0 C9 D. l/ m - this.id = id;6 }/ L5 `& W, o( U. X) M: s- g
- }; N4 ?" V- j/ W. E9 K' P+ q
8 b% L; n3 |- Q9 x- public String getName()- ~/ E- g1 V: V( [1 o% d0 B
- {# w! _8 |. L2 X
- return name;
! K- _$ o% o7 \1 ^7 s3 l! ^ - }3 `) K* Y6 E5 }6 E9 j
- : a! k* F4 S5 @
- public void setName(String name)! ~8 M/ v4 n3 K. k
- {2 ^ ^6 l& ~& V Y- O
- this.name = name;
1 c/ F' Z& a6 w/ H - }
$ [( t K/ u" U7 b8 P- R2 F3 v$ {( V; R% M - ; a, z8 E: [/ [ v9 P( J8 \" H' U5 ]* i3 m
- public int getAge()
% C) G6 P+ J0 ?0 O f - {, a* g" t+ i& O2 s* l [7 j+ [$ n
- return age;% s6 w% K( }" Z
- }' v1 }0 r' r7 H" H% n. j$ ]
( |6 C ^7 A" {; @7 Q5 I- public void setAge(int age)
5 x I3 N' c# }" ^7 e - {5 M7 o, D0 @8 E, U3 \% o- K, k
- this.age = age;! j# h* z q2 q z& [
- }
! P! o8 R l% I, _' p - 0 L" i6 b* m% h6 R% k5 k
- @Override
3 q4 Y' I" Y, j3 ?( L2 B7 K - public int hashCode()
# Z T+ @4 R8 t E6 z! Z$ U - {
" F! ~# u0 J* E+ J" o6 }# H% D; Y - final int prime = 31;
# C s6 N/ M0 u - int result = 1;( n; R, B; q, d0 J$ u+ B5 b
- result = prime * result + ((id == null) ? 0 : id.hashCode());1 |4 ]1 l5 e% A7 Y8 [) D' f1 O/ w3 K
- result = prime * result + ((name == null) ? 0 : name.hashCode());: V: q' U+ {8 F/ t5 W* _
- return result;
+ t) U6 ~) T; R8 j2 R - }
) T, H7 c* Y; ^+ \) h5 R" q% G! d0 N - ( v& c n. ^! H; `: U
- @Override
& i; U" Z# y1 _/ G! j8 @( ? - public boolean equals(Object obj), w- a- \ U8 i$ h
- {0 M1 j4 ^. j X; n
- if (this == obj): Y9 |, y3 _+ P4 D5 g- T8 ]
- return true;
" V& t9 l7 R6 d* b A4 {# J - if (obj == null)" g5 \' d& w1 K: `
- return false;! O! m2 `# K" _* g+ n$ P
- if (getClass() != obj.getClass()): o% ]& N/ S K! W: a, u
- return false;
* u- ~1 Z9 o; [/ q4 ]6 F, l - People other = (People) obj;( `3 ]- |( P7 g7 e
- if (id == null)) O- s+ i8 Z. P; M+ }) @
- {
. C- {2 E: B) r* C! O: a9 m - if (other.id != null)/ u) Z6 H$ `5 Q( F1 g# ]
- return false;
% X, D2 H) Q' n4 A- e - }
5 H3 O: H% B# U$ R; |7 ^7 H( p - else if (!id.equals(other.id))
$ ~; x6 k6 l6 M! g: m - return false;
# e7 T& z+ M# b - if (name == null)
: \- n. \5 n4 z7 k8 V6 P& k4 Y - {
7 [* c9 P! h5 q - if (other.name != null)/ E. v1 }3 M% I$ E
- return false;8 I9 k) a; _/ z `: [
- }, S0 M: H ?' e
- else if (!name.equals(other.name))2 _1 g* ] o) n+ T/ z
- return false;
$ W/ _" d9 w' B. X1 s! o - return true;
+ a( h3 t0 a( @9 V8 }) U - }
" [1 e2 P2 }* S6 V- N - }
复制代码
! v9 V) J3 x0 N# |
: s" c% }6 C9 W6 ]; Y; J
" n5 {* N3 v9 z+ s9 t5 G* c1 @. x1 \7 q# C- E
|
|