GenericEntity.java
2.25 KB
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
package fi.insomnia.bortal.model;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.persistence.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.insomnia.bortal.utilities.PasswordFunctions;
@MappedSuperclass
public class GenericEntity implements ModelInterface {
private static final Logger logger = LoggerFactory.getLogger(GenericEntity.class);
/**
*
*/
private static final long serialVersionUID = -9041737052951021560L;
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Version
@Column(nullable = false)
private int jpaVersionField = 0;
@Override
public final boolean equals(Object object) {
if (!this.getClass().equals(object.getClass())) {
logger.debug("Classes dont match {} {}", getClass(), object.getClass());
return false;
}
GenericEntity other = (GenericEntity) object;
if (tempId != null && tempId.equals(other.tempId)) {
return true;
}
if (this.id == null) {
if (other.id != null) {
return false;
}
logger.debug("Ids are null. Resorting to randomidcheck!");
return getRandomId().equals(other.tempId);
}
return this.id.equals(other.id);
}
@Transient
private String tempId;
private String getRandomId() {
if (tempId == null) {
tempId = PasswordFunctions.generateRandomString(10);
}
return tempId;
}
@Override
public final int getJpaVersionField() {
return jpaVersionField;
}
@Override
public final void setJpaVersionField(int jpaVersionField) {
this.jpaVersionField = jpaVersionField;
}
@Override
public final int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : getRandomId().hashCode());
return hash;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}