-
Notifications
You must be signed in to change notification settings - Fork 22
/
Account.java
174 lines (142 loc) · 4.76 KB
/
Account.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Source code generated by Celerio, a Jaxio product.
* Documentation: http://www.jaxio.com/documentation/celerio/
* Follow us on twitter: @jaxiosoft
* Need commercial support ? Contact us: [email protected]
* Template pack-backend-jpa:src/main/java/domain/Entity.e.vm.java
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
*/
package demo;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.jaxio.jpa.querybyexample.Identifiable;
import org.hibernate.annotations.GenericGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import java.io.Serializable;
import java.util.Date;
import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.TemporalType.TIMESTAMP;
@Entity
@Table(name = "ACCOUNT")
public class Account implements Identifiable<Integer>, Serializable {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(Account.class);
// Raw attributes
private Integer id;
private String username;
private String lastName;
private Date birthDate;
// Many to one
private Address homeAddress;
@Override
@Column(name = "ID", precision = 10)
@GeneratedValue
@Id
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
@Override
@Transient
public boolean isIdSet() {
return id != null;
}
// -- [username] ------------------------
@Column(name = "USERNAME", nullable = false, unique = true, length = 100)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Account username(String username) {
setUsername(username);
return this;
}
// -- [lastName] ------------------------
@Column(name = "LAST_NAME", length = 255)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account lastName(String lastName) {
setLastName(lastName);
return this;
}
// -- [birthDate] ------------------------
@Column(name = "BIRTH_DATE", length = 23)
@Temporal(TIMESTAMP)
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Account birthDate(Date birthDate) {
setBirthDate(birthDate);
return this;
}
// -----------------------------------------------------------------
// Many to One support
// -----------------------------------------------------------------
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// many-to-one: Account.homeAddress ==> Address.id
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@JoinColumn(name = "ADDRESS_ID")
@ManyToOne(cascade = { PERSIST, MERGE }, fetch = LAZY)
public Address getHomeAddress() {
return homeAddress;
}
/**
* Set the {@link #homeAddress} without adding this Account instance on the passed {@link #homeAddress}
*/
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
public Account homeAddress(Address homeAddress) {
setHomeAddress(homeAddress);
return this;
}
/**
* Equals implementation using a business key.
*/
@Override
public boolean equals(Object other) {
return this == other || (other instanceof Account && hashCode() == other.hashCode());
}
private volatile int previousHashCode = 0;
@Override
public int hashCode() {
int hashCode = Objects.hashCode(getUsername());
if (previousHashCode != 0 && previousHashCode != hashCode) {
log.warn("DEVELOPER: hashCode has changed!." //
+ "If you encounter this message you should take the time to carefully review equals/hashCode for: " //
+ getClass().getCanonicalName());
}
previousHashCode = hashCode;
return hashCode;
}
/**
* Construct a readable string representation for this Account instance.
*
* @see Object#toString()
*/
@Override
public String toString() {
return MoreObjects.toStringHelper(this) //
.add("id", getId()) //
.add("username", getUsername()) //
.add("birthDate", getBirthDate()) //
.toString();
}
}