-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSQLEntity.java
159 lines (139 loc) · 4.49 KB
/
SQLEntity.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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.db.jdbc;
import sirius.db.mixing.BaseEntity;
import sirius.db.mixing.BaseMapper;
import sirius.db.mixing.Mapping;
import sirius.db.mixing.annotations.Transient;
import sirius.db.mixing.query.Query;
import sirius.db.mixing.query.constraints.Constraint;
import sirius.kernel.di.std.Part;
import java.util.Objects;
/**
* Represents the base class for all entities which are managed via {@link sirius.db.jdbc.OMA} and stored in
* a JDBC / SQL database.
*/
public abstract class SQLEntity extends BaseEntity<Long> {
/**
* Represents the id which signals that the entity was not persisted yet.
*/
public static final long NON_PERSISTENT_ENTITY_ID = -1;
@Part
protected static OMA oma;
/**
* Contains the unique {@link #ID} of the entity.
* <p>
* This is automatically assigned by the database. Never assign manually a value unless you are totally aware
* of what you're doing. Also, you should not use this ID for external purposes (e.g. as customer number or
* invoice number) as it cannot be changed.
*/
protected long id = NON_PERSISTENT_ENTITY_ID;
@Transient
protected int version = 0;
@Transient
protected Row fetchRow;
@Override
public boolean isNew() {
return id < 0;
}
@SuppressWarnings("unchecked")
@Override
public <E extends BaseEntity<?>, C extends Constraint, Q extends Query<Q, E, C>> BaseMapper<E, C, Q> getMapper() {
return (BaseMapper<E, C, Q>) oma;
}
/**
* Returns the ID of the entity.
*
* @return the ID of the entity or a negative value if the entity was not written to the database yet
*/
@Override
public Long getId() {
return id;
}
/**
* Sets the ID of the entity.
* <p>
* This method must be used very carefully. For normal operations, this method should ne be used at all
* as the database ID is managed by the framework.
*
* @param id the id of the entity in the database
*/
public void setId(long id) {
this.id = id;
}
/**
* Returns the version of the entity.
*
* @return the version of the entity
*/
public int getVersion() {
return version;
}
/**
* Note that only the framework must use this to specify the version of the entity.
*
* @param version the version of this entity
*/
public void setVersion(int version) {
this.version = version;
}
/**
* Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those
* provided by {@link java.util.HashMap}.
* <p>
* The hash code of an entity is based on its ID. If the entity is not written to the database yet, we use
* the hash code as computed by {@link Object#hashCode()}. This matches the behaviour of {@link #equals(Object)}.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
if (id < 0) {
// Return a hash code appropriate to the implementation of equals.
return super.hashCode();
}
return (int) (id % Integer.MAX_VALUE);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (other.getClass() != this.getClass()) {
return false;
}
SQLEntity otherEntity = (SQLEntity) other;
if (isNew()) {
return otherEntity.isNew() && super.equals(other);
}
return Objects.equals(getId(), otherEntity.getId());
}
@Override
public boolean isUnique(Mapping field, Object value, Mapping... within) {
SmartQuery<? extends SQLEntity> qry = oma.select(getClass()).eq(field, value);
for (Mapping withinField : within) {
qry.eq(withinField, getDescriptor().getProperty(withinField).getValue(this));
}
if (!isNew()) {
qry.ne(ID, getId());
}
return !qry.exists();
}
/**
* If the entity was created as the result of a {@link TransformedQuery}, the original {@link Row} is
* accessible via this method.
*
* @return the original row from which the entity was loaded.
*/
public Row getFetchRow() {
return fetchRow;
}
}