public class EnumToStringTranslator<T extends java.lang.Enum<T>> extends EnumTranslator<T>
TypeTranslator
to use for Enum fields. Writes Enum (Enum.toString()
)
to column as a String. Override Enum.toString()
to supply a value to store
in the database. By default Enum.toString()
returns Enum.name()
.
Column should be a sql type that is compatible with java String (like varchar) and
wide enough for the longest value returned by Enum.toString()
.
This class and subclasses may be used as a TypeTranslator
for Enum fields
in one of two ways:
ImplicitType.translator()
or ExplicitType.translator()
.EnumType.translator()
.Constructor and Description |
---|
EnumToStringTranslator() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Enum<T> |
columnToEnum(java.lang.Object columnValue)
Converts a table column value to an Enum.
|
protected java.lang.Object |
enumToColumn(java.lang.Enum<T> enumValue)
Converts an Enum constant to column value with
Enum.toString() . |
java.util.Map<java.lang.Object,java.lang.Enum<T>> |
getColumnEnumMap()
Gets the map of Enum's that were built by
setEnumClass(Class) for use
in converting database values to Enum's in columnToEnum(Object) . |
void |
setEnumClass(java.lang.Class<T> enumClass)
Sets the class of Enum to translate.
|
getDefaultEnum, getDefaultEnumName, getEnumClass, read, setDefaultEnumName, write
public void setEnumClass(java.lang.Class<T> enumClass)
RowTranslator
to
initialize this translator when Table
object is initialized that
contains an Enum field.
This method also builds a map of Enums where keys are enumToColumn(Enum)
for all elements of Class.getEnumConstants()
. Map is used by columnToEnum(Object)
to convert from column value to Enum.
setEnumClass
in class EnumTranslator<T extends java.lang.Enum<T>>
enumClass
- Class of Enum to be translatedpublic java.util.Map<java.lang.Object,java.lang.Enum<T>> getColumnEnumMap()
setEnumClass(Class)
for use
in converting database values to Enum's in columnToEnum(Object)
.enumToColumn(Enum)
protected java.lang.Object enumToColumn(java.lang.Enum<T> enumValue)
Enum.toString()
. Override
this method if something other than Enum.toString()
should be used for
database value of Enum.enumToColumn
in class EnumTranslator<T extends java.lang.Enum<T>>
enumValue
- the Enum constant to convertEnum.toString()
; null if enumValue is nullprotected java.lang.Enum<T> columnToEnum(java.lang.Object columnValue)
setEnumClass(Class)
. Key
to map is enumToColumn(Enum)
.
This method may be overridden if desired. It is not likely that
you will need to override if you've overridden enumToColumn(Enum)
.
columnToEnum
in class EnumTranslator<T extends java.lang.Enum<T>>
columnValue
- value read from table column that stores EnumenumToColumn(Enum)
equals columnValue;
null if columnValue is null; EnumTranslator.getDefaultEnum()
if columnValue
cannot be found in getColumnEnumMap()