/
User.java
154 lines (117 loc) · 3.88 KB
/
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package yoyoyousei.twitter.clone.domain.model;
import yoyoyousei.twitter.clone.util.Util;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;
//デフォルトコンストラクタが必要
@Entity
//@Table(name = "usr")
public class User{
/*@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;*/
@Id
@NotNull
//@Column(unique = true)
private String userId;
@NotNull
private String password;
@NotNull
private String screenName;
@NotNull
private RoleName roleName;
private String biography;
private String iconPath;
//双方向ならmappedbyが必要で、どのプロパティと関連するのか指定する必要がある。
@OneToMany(mappedBy = "tweetUser")
private List<Tweet> tweets;
//fetchtype: Eager フィールドの呼び出しを最初の呼び出しで行う lazy:フィールドにアクセスが合った時点で
//cascade: このプロパティをどのように変更した際に関連するentityに変更を反映するか
//persist:新規保存 merge:更新 remove:削除 refresh:再取得したとき detatch:永続性コンテキストの管理外になったとき all:全て
//cascade={hoge,fuga}と複数指定できる
//勝手に反映してほしくないしcascade不要
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "relation",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "following_id"))
private List<User> following;
public User(String userId, String password, String screenName) {
this.userId = userId;
this.password = password;
this.screenName = (screenName == null || screenName.equals("")) ?
"no name" : screenName;
this.roleName = RoleName.USER;
this.iconPath = "/images/noicon.png"; //Util.getNoIcon();
}
public User() {
}
public List<Tweet> getTweets() {
return tweets;
}
public void setTweets(List<Tweet> tweets) {
this.tweets = tweets;
}
public List<User> getFollowing() {
return following;
}
public void setFollowing(List<User> following) {
this.following = following;
}
public String getIconPath() {
return iconPath;
}
public void setIconPath(String iconPath) {
this.iconPath = iconPath;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
public RoleName getRoleName() {
return roleName;
}
public void setRoleName(RoleName roleName) {
this.roleName = roleName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getScreenName() {
return screenName;
}
public void setScreenName(String screenName) {
this.screenName = screenName;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", password='" + password + '\'' +
", screenName='" + screenName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (!getUserId().equals(user.getUserId())) return false;
return getPassword().equals(user.getPassword());
}
@Override
public int hashCode() {
int result = getUserId().hashCode();
result = 31 * result + getPassword().hashCode();
return result;
}
}