forked from mrigor87/CalorieManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseEntity.java
More file actions
69 lines (57 loc) · 1.83 KB
/
BaseEntity.java
File metadata and controls
69 lines (57 loc) · 1.83 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
package ru.javawebinar.topjava.model;
import org.hibernate.Hibernate;
import org.springframework.data.domain.Persistable;
import javax.persistence.*;
/**
* User: gkislin
* Date: 22.08.2014
* <p>
* Do not manipulate new (transient) entries in HashSet/HashMap without overriding hashCode
* http://stackoverflow.com/a/39827962/548473
*
* @see org.springframework.data.jpa.domain.AbstractPersistable
*/
@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 class BaseEntity implements Persistable<Integer> {
public static final int START_SEQ = 100000;
@Id
@SequenceGenerator(name = "global_seq", sequenceName = "global_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_seq")
// PROPERTY access for id due to bug: https://hibernate.atlassian.net/browse/HHH-3718
@Access(value = AccessType.PROPERTY)
protected Integer id;
public BaseEntity() {
}
protected BaseEntity(Integer id) {
this.id = id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public Integer getId() {
return id;
}
@Override
public boolean isNew() {
return (getId() == null);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !getClass().equals(Hibernate.getClass(o))) {
return false;
}
BaseEntity that = (BaseEntity) o;
return null != getId() && getId().equals(that.getId());
}
@Override
public int hashCode() {
return (getId() == null) ? 0 : getId();
}
}