forked from knastnt/JavaRush-Training-Topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractBaseEntity.java
More file actions
74 lines (60 loc) · 2.27 KB
/
AbstractBaseEntity.java
File metadata and controls
74 lines (60 loc) · 2.27 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
package ru.javawebinar.topjava.model;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.springframework.util.Assert;
import org.springframework.data.domain.Persistable;
import org.hibernate.Hibernate;
import ru.javawebinar.topjava.HasId;
import javax.persistence.*;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
@MappedSuperclass
// http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access
@Access(AccessType.FIELD)
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, isGetterVisibility = NONE, setterVisibility = NONE)
public abstract class AbstractBaseEntity implements HasId {
public static final int START_SEQ = 100000;
@Id
@SequenceGenerator(name = "global_seq", sequenceName = "global_seq", allocationSize = 1, initialValue = START_SEQ)
// @Column(name = "id", unique = true, nullable = false, columnDefinition = "integer default nextval('global_seq')")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_seq")
// See https://hibernate.atlassian.net/browse/HHH-3718 and https://hibernate.atlassian.net/browse/HHH-12034
// Proxy initialization when accessing its identifier managed now by JPA_PROXY_COMPLIANCE setting
protected Integer id;
protected AbstractBaseEntity() {
}
protected AbstractBaseEntity(Integer id) {
this.id = id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
@Override
public Integer getId() {
return id;
}
// doesn't work for hibernate lazy proxy
public int id() {
Assert.notNull(id, "Entity must has id");
return id;
}
@Override
public String toString() {
return getClass().getSimpleName() + ":" + id;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !getClass().equals(Hibernate.getClass(o))) {
return false;
}
AbstractBaseEntity that = (AbstractBaseEntity) o;
return id != null && id.equals(that.id);
}
@Override
public int hashCode() {
return id == null ? 0 : id;
}
}