Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
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
/*
* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
*/
package net.curisit.securis.db.common;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
/**
* PersistentEnumUserType
* <p>
* Base Hibernate {@link UserType} for enums implementing {@link CodedEnum}.
* Stores the enum's <b>short code</b> (VARCHAR) and reconstructs the enum
* from that code on load.
*
* Notes:
* - SQL type is {@code VARCHAR}.
* - Immutable by default ({@link #isMutable()} returns false).
* - {@link #equals(Object, Object)} compares by reference (adequate for enums).
*
* @param <T> enum type implementing {@link CodedEnum}
*
* @author JRA
* Last reviewed by JRA on Oct 7, 2025.
*/
public abstract class PersistentEnumUserType<T extends CodedEnum> implements UserType {
    /** 
     * assemble<p>
     * Return cached value as-is (immutable semantics). 
     * 
     * @param cached
     * @param owner
     * @return assembleObject
     * @throws HibernateException
     */
    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }
    /** 
     * deepCopy<p>
     * Enums are immutable; return value as-is. 
     * 
     * @param value
     * @return deepCopy
     */
    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }
    /** 
     * disassemble<p>
     * Return value for 2nd-level cache.
     * 
     * @param value
     * @return disassembleObject
     * @throw HibernateException
     */
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }
    /** 
     * equals<p>
     * Reference equality is fine for enums.
     * 
     * @param x
     * @param y
     * @param isEqual
     * @throws HibernateException
     */
    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        return x == y;
    }
    /** 
     * hashCode<p>
     * Delegate to value hashCode; 0 for null. 
     * 
     * @param object
     * @return hashCode
     * @throws HibernateException
     */
    @Override
    public int hashCode(Object x) throws HibernateException {
        return x == null ? 0 : x.hashCode();
    }
    /** 
     * isMutable<p>
     * Enums are immutable. 
     * 
     * @return isMutable
     */
    @Override
    public boolean isMutable() {
        return false;
    }
    /** 
     * replace<p>
     * Immutable; return original. 
     * 
     * @param original
     * @param target
     * @param owner
     * @return object
     * @throws HibernateException
     */
    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }
    /** 
     * returnedClass<p>
     * Concrete types must provide the enum class. 
     */
    @Override
    public abstract Class<T> returnedClass();
    /** 
     * sqlTypes<p>
     * Persist as single VARCHAR column
     * 
     * @return sqlTypes
     */
    @Override
    public int[] sqlTypes() {
        return new int[] { Types.VARCHAR };
    }
    /**
    * nullSafeGet<p>
    * Read code from result set and map to the corresponding enum constant.
    *
    * @param resultSet
    * @param names
    * @param session
    * @param owner
    * @return enum instance or null if DB value is null or code not matched
    */
    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
            throws HibernateException, SQLException {
        String code = rs.getString(names[0]);
        if (code == null) return null;
        for (CodedEnum en : returnedClass().getEnumConstants()) {
            if (en.getCode().equals(code)) {
                return en;
            }
        }
        return null;
    }
    /**
    * nullSafeSet<p>
    * Write enum code as VARCHAR or set NULL if value is null.
    * 
    * @param statement
    * @param value
    * @param index
    * @param session
    */
    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
            throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, java.sql.Types.VARCHAR);
        } else {
            st.setString(index, ((CodedEnum) value).getCode());
        }
    }
}