/
ImmutableType.java
349 lines (287 loc) · 10.5 KB
/
ImmutableType.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.vladmihalcea.hibernate.type;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.util.ReflectionUtils;
import org.dom4j.Node;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
/**
* Very convenient base class for implementing immutable object types using Hibernate {@link UserType}.
* <p>
* The {@link ImmutableType} implements the {@link Type} interface too, so you can pass all
* types extending the {@link ImmutableType} to the {@link org.hibernate.SQLQuery#addScalar(String, Type)}
* method to fix the <a href="https://vladmihalcea.com/hibernate-no-dialect-mapping-for-jdbc-type/">No Dialect mapping for JDBC type</a> issues.
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableType<T> implements UserType, BasicType {
private final Configuration configuration;
private final Class<T> clazz;
/**
* Initialization constructor taking the {@link Class}
* and using the default {@link Configuration} object.
*
* @param clazz the entity attribute {@link Class} type to be handled
*/
protected ImmutableType(Class<T> clazz) {
this.clazz = clazz;
this.configuration = Configuration.INSTANCE;
}
/**
* Initialization constructor taking the {@link Class} and {@link Configuration} objects.
*
* @param clazz the entity attribute {@link Class} type to be handled
* @param configuration custom {@link Configuration} object.
*/
protected ImmutableType(Class<T> clazz, Configuration configuration) {
this.clazz = clazz;
this.configuration = configuration;
}
/**
* Get the current {@link Configuration} object.
*
* @return the current {@link Configuration} object.
*/
protected Configuration getConfiguration() {
return configuration;
}
/**
* Get the column value from the JDBC {@link ResultSet}.
*
* @param rs JDBC {@link ResultSet}
* @param names database column name
* @param session current Hibernate {@link org.hibernate.Session}
* @param owner current Hibernate {@link org.hibernate.SessionFactory}
* @return column value
* @throws SQLException in case of failure
*/
protected abstract T get(ResultSet rs, String[] names,
SessionImplementor session, Object owner) throws SQLException;
/**
* Set the column value on the provided JDBC {@link PreparedStatement}.
*
* @param st JDBC {@link PreparedStatement}
* @param value database column value
* @param index database column index
* @param session current Hibernate {@link org.hibernate.Session}
* @throws SQLException in case of failure
*/
protected abstract void set(PreparedStatement st, T value, int index,
SessionImplementor session) throws SQLException;
/* Methods inherited from the {@link UserType} interface */
@Override
public Object nullSafeGet(ResultSet rs, String[] names,
SessionImplementor session, Object owner) throws SQLException {
return get(rs, names, session, owner);
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
SessionImplementor session) throws SQLException {
set(st, clazz.cast(value), index, session);
}
@Override
public Class<T> returnedClass() {
return clazz;
}
@Override
public boolean equals(Object x, Object y) {
return (x == y) || (x != null && x.equals(y));
}
@Override
public int hashCode(Object x) {
return x.hashCode();
}
@Override
public Object deepCopy(Object value) {
return value;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object o) {
return (Serializable) o;
}
@Override
public Object assemble(Serializable cached, Object owner) {
return cached;
}
@Override
public Object replace(Object o, Object target, Object owner) {
return o;
}
/* Methods inherited from the {@link Type} interface */
@Override
public boolean isAssociationType() {
return false;
}
@Override
public boolean isCollectionType() {
return false;
}
@Override
public boolean isEntityType() {
return false;
}
@Override
public boolean isAnyType() {
return false;
}
@Override
public boolean isComponentType() {
return false;
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return 1;
}
@Override
public int[] sqlTypes(Mapping mapping) throws MappingException {
return sqlTypes();
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[]{new Size()};
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return dictatedSizes(mapping);
}
@Override
public Class getReturnedClass() {
return returnedClass();
}
@Override
public boolean isSame(Object x, Object y) throws HibernateException {
return equals(x, y);
}
@Override
public boolean isEqual(Object x, Object y) throws HibernateException {
return equals(x, y);
}
@Override
public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) throws HibernateException {
return equals(x, y);
}
@Override
public int getHashCode(Object x) throws HibernateException {
return hashCode(x);
}
@Override
public int getHashCode(Object x, SessionFactoryImplementor factory) throws HibernateException {
return hashCode(x);
}
@Override
public int compare(Object x, Object y) {
return IncomparableComparator.INSTANCE.compare(x, y);
}
@Override
public final boolean isDirty(Object old, Object current, SessionImplementor session) {
return isDirty(old, current);
}
@Override
public final boolean isDirty(Object old, Object current, boolean[] checkable, SessionImplementor session) {
return checkable[0] && isDirty(old, current);
}
protected final boolean isDirty(Object old, Object current) {
return !isSame(old, current);
}
@Override
public boolean isModified(Object dbState, Object currentState, boolean[] checkable, SessionImplementor session) throws HibernateException {
return isDirty(dbState, currentState);
}
@Override
public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) throws HibernateException, SQLException {
return get(rs, new String[]{name}, session, owner);
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) throws HibernateException, SQLException {
set(st, returnedClass().cast(value), index, session);
}
@Override
public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException {
return String.valueOf(value);
}
@Override
public String getName() {
return getClass().getSimpleName();
}
@Override
public Object deepCopy(Object value, SessionFactoryImplementor factory) throws HibernateException {
return deepCopy(value);
}
@Override
public Serializable disassemble(Object value, SessionImplementor session, Object owner) throws HibernateException {
return disassemble(value);
}
@Override
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
return assemble(cached, session);
}
@Override
public void beforeAssemble(Serializable cached, SessionImplementor session) {
}
@Override
public Object hydrate(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
return nullSafeGet(rs, names, session, owner);
}
@Override
public Object resolve(Object value, SessionImplementor session, Object owner) throws HibernateException {
return value;
}
@Override
public Object semiResolve(Object value, SessionImplementor session, Object owner) throws HibernateException {
return value;
}
@Override
public Type getSemiResolvedType(SessionFactoryImplementor factory) {
return this;
}
@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) throws HibernateException {
return replace(original, target, owner);
}
@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache, ForeignKeyDirection foreignKeyDirection) throws HibernateException {
return replace(original, target, owner);
}
@Override
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}
@Override
public boolean isXMLElement() {
return false;
}
@Override
public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException {
node.setText(toLoggableString(value, factory));
}
@Override
public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
Method valueOfMethod = ReflectionUtils.getMethodOrNull(clazz, "valueOf", String.class);
return valueOfMethod != null ? ReflectionUtils.invokeStaticMethod(valueOfMethod, xml.getText()) : null;
}
@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}