Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/db/common/PersistentEnumUserType.java
....@@ -1,3 +1,6 @@
1
+/*
2
+* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+*/
14 package net.curisit.securis.db.common;
25
36 import java.io.Serializable;
....@@ -10,57 +13,150 @@
1013 import org.hibernate.engine.spi.SharedSessionContractImplementor;
1114 import org.hibernate.usertype.UserType;
1215
16
+/**
17
+* PersistentEnumUserType
18
+* <p>
19
+* Base Hibernate {@link UserType} for enums implementing {@link CodedEnum}.
20
+* Stores the enum's <b>short code</b> (VARCHAR) and reconstructs the enum
21
+* from that code on load.
22
+*
23
+* Notes:
24
+* - SQL type is {@code VARCHAR}.
25
+* - Immutable by default ({@link #isMutable()} returns false).
26
+* - {@link #equals(Object, Object)} compares by reference (adequate for enums).
27
+*
28
+* @param <T> enum type implementing {@link CodedEnum}
29
+*
30
+* @author JRA
31
+* Last reviewed by JRA on Oct 7, 2025.
32
+*/
1333 public abstract class PersistentEnumUserType<T extends CodedEnum> implements UserType {
1434
35
+ /**
36
+ * assemble<p>
37
+ * Return cached value as-is (immutable semantics).
38
+ *
39
+ * @param cached
40
+ * @param owner
41
+ * @return assembleObject
42
+ * @throws HibernateException
43
+ */
1544 @Override
1645 public Object assemble(Serializable cached, Object owner) throws HibernateException {
1746 return cached;
1847 }
1948
49
+ /**
50
+ * deepCopy<p>
51
+ * Enums are immutable; return value as-is.
52
+ *
53
+ * @param value
54
+ * @return deepCopy
55
+ */
2056 @Override
2157 public Object deepCopy(Object value) throws HibernateException {
2258 return value;
2359 }
2460
61
+ /**
62
+ * disassemble<p>
63
+ * Return value for 2nd-level cache.
64
+ *
65
+ * @param value
66
+ * @return disassembleObject
67
+ * @throw HibernateException
68
+ */
2569 @Override
2670 public Serializable disassemble(Object value) throws HibernateException {
2771 return (Serializable) value;
2872 }
2973
74
+ /**
75
+ * equals<p>
76
+ * Reference equality is fine for enums.
77
+ *
78
+ * @param x
79
+ * @param y
80
+ * @param isEqual
81
+ * @throws HibernateException
82
+ */
3083 @Override
3184 public boolean equals(Object x, Object y) throws HibernateException {
3285 return x == y;
3386 }
3487
88
+ /**
89
+ * hashCode<p>
90
+ * Delegate to value hashCode; 0 for null.
91
+ *
92
+ * @param object
93
+ * @return hashCode
94
+ * @throws HibernateException
95
+ */
3596 @Override
3697 public int hashCode(Object x) throws HibernateException {
3798 return x == null ? 0 : x.hashCode();
3899 }
39100
101
+ /**
102
+ * isMutable<p>
103
+ * Enums are immutable.
104
+ *
105
+ * @return isMutable
106
+ */
40107 @Override
41108 public boolean isMutable() {
42109 return false;
43110 }
44111
112
+ /**
113
+ * replace<p>
114
+ * Immutable; return original.
115
+ *
116
+ * @param original
117
+ * @param target
118
+ * @param owner
119
+ * @return object
120
+ * @throws HibernateException
121
+ */
45122 @Override
46123 public Object replace(Object original, Object target, Object owner) throws HibernateException {
47124 return original;
48125 }
49126
127
+ /**
128
+ * returnedClass<p>
129
+ * Concrete types must provide the enum class.
130
+ */
50131 @Override
51132 public abstract Class<T> returnedClass();
52133
134
+ /**
135
+ * sqlTypes<p>
136
+ * Persist as single VARCHAR column
137
+ *
138
+ * @return sqlTypes
139
+ */
53140 @Override
54141 public int[] sqlTypes() {
55
- return new int[] {
56
- Types.VARCHAR
57
- };
142
+ return new int[] { Types.VARCHAR };
58143 }
59144
145
+ /**
146
+ * nullSafeGet<p>
147
+ * Read code from result set and map to the corresponding enum constant.
148
+ *
149
+ * @param resultSet
150
+ * @param names
151
+ * @param session
152
+ * @param owner
153
+ * @return enum instance or null if DB value is null or code not matched
154
+ */
60155 @Override
61156 public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
62157 throws HibernateException, SQLException {
63158 String code = rs.getString(names[0]);
159
+ if (code == null) return null;
64160 for (CodedEnum en : returnedClass().getEnumConstants()) {
65161 if (en.getCode().equals(code)) {
66162 return en;
....@@ -69,6 +165,15 @@
69165 return null;
70166 }
71167
168
+ /**
169
+ * nullSafeSet<p>
170
+ * Write enum code as VARCHAR or set NULL if value is null.
171
+ *
172
+ * @param statement
173
+ * @param value
174
+ * @param index
175
+ * @param session
176
+ */
72177 @Override
73178 public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
74179 throws HibernateException, SQLException {
....@@ -78,5 +183,5 @@
78183 st.setString(index, ((CodedEnum) value).getCode());
79184 }
80185 }
81
-
82186 }
187
+