/* * 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 *

* Base Hibernate {@link UserType} for enums implementing {@link CodedEnum}. * Stores the enum's short code (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 enum type implementing {@link CodedEnum} * * @author JRA * Last reviewed by JRA on Oct 7, 2025. */ public abstract class PersistentEnumUserType implements UserType { /** * assemble

* 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

* Enums are immutable; return value as-is. * * @param value * @return deepCopy */ @Override public Object deepCopy(Object value) throws HibernateException { return value; } /** * disassemble

* Return value for 2nd-level cache. * * @param value * @return disassembleObject * @throw HibernateException */ @Override public Serializable disassemble(Object value) throws HibernateException { return (Serializable) value; } /** * equals

* 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

* 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

* Enums are immutable. * * @return isMutable */ @Override public boolean isMutable() { return false; } /** * replace

* 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

* Concrete types must provide the enum class. */ @Override public abstract Class returnedClass(); /** * sqlTypes

* Persist as single VARCHAR column * * @return sqlTypes */ @Override public int[] sqlTypes() { return new int[] { Types.VARCHAR }; } /** * nullSafeGet

* 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

* 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()); } } }