zj
2024-06-03 3603ecb207f7e712c635f19531e05fac4d19e53f
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
package kernel.bo;
 
import java.io.Serializable;
import java.util.Date;
 
import javax.persistence.Column;
import javax.persistence.Id;
 
import org.springframework.util.ClassUtils;
 
/**
 * 实体抽象类
 * 
 */
public  class EntityObject implements Serializable {
 
    private static final long serialVersionUID = -6624393812017741464L;
 
    @Id
    @Column(name="UUID")
    private Serializable id;
    
    private int entityVersion;
    
    private Date timestamp;
 
    public Serializable getId() {
        return id;
    }
 
    public void setId(Serializable id) {
        this.id = id;
    }
 
    public int getEntityVersion() {
        return entityVersion;
    }
 
    public void setEntityVersion(int entityVersion) {
        this.entityVersion = entityVersion;
    }
 
    public Date getTimestamp() {
        return timestamp;
    }
 
    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
 
    // @Override
    public String toString() {
        return ClassUtils.getShortName(getClass()) + ": id=" + getId();
    }
 
    /**
     * Attempt to establish identity based on id if both exist. If either id
     * does not exist use Object.equals().
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (other == null) {
            return false;
        }
        if (!(other instanceof EntityObject)) {
            return false;
        }
        // 即便是继承关系,由于数据逻辑存在依赖关系,必须是同class才行
        if(!this.getClass().equals(other.getClass())){
            return false;
        }
        EntityObject entity = (EntityObject) other;
        if (id == null || entity.getId() == null) {
            return false;
        }
        return id.equals(entity.getId());
    }
 
    /**
     * Use ID if it exists to establish hash code, otherwise fall back to
     * Object.hashCode(). Based on the same information as equals, so if that
     * changes, this will. N.B. this follows the contract of Object.hashCode(),
     * but will cause problems for anyone adding an unsaved {@link Entity} to a
     * Set because Set.contains() will almost certainly return false for the
     * {@link Entity} after it is saved. Spring Batch does not store any of its
     * entities in Sets as a matter of course, so internally this is consistent.
     * Clients should not be exposed to unsaved entities.
     * 
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        if (id == null) {
            return super.hashCode();
        }
        return 39 + 29 * getClass().hashCode() + 87 * id.hashCode();
    }
 
}