-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.java
117 lines (98 loc) · 2.57 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
package fetching.model;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "USERS")
@NamedQuery(name = User.FIND_ALL, query = "select u from User u")
public class User {
public static final String FIND_ALL = "User.findAll";
@Id
@GeneratedValue
@Column(name = "user_id")
private Long id;
@Column(name = "user_name")
private String userName;
@Column(name = "user_phone")
private String phone;
@OneToMany(
fetch = FetchType.LAZY,
orphanRemoval = true,
cascade = CascadeType.ALL
)
@JoinTable(
name = "user_on_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
private List<Role> roles;
public Long getId() {
return id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return Objects.equals(id, user.id) && Objects.equals(userName, user.userName)
&& Objects.equals(phone, user.phone) && Objects.equals(roles, user.roles);
}
@Override
public int hashCode() {
return Objects.hash(id, userName, phone, roles);
}
/*
incorrect toString in case of lazy initializing,
because if we will use roles in toString hibernate
select this during creation of entity (it can find/named query etc.).
*/
// @Override
// public String toString() {
// return "User{" +
// "id=" + id +
// ", userName='" + userName + '\'' +
// ", phone='" + phone + '\'' +
// ", roles=" + roles +
// '}';
// }
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", phone='" + phone + '\'' +
'}';
}
}