public class EnumTranslator<T extends java.lang.Enum<T>> extends java.lang.Object implements TypeTranslator<java.lang.Enum<T>>
TypeTranslator
to use for Enum fields. Writes Enum name (Enum.name()
)
to column as a String. Reads column as a String Enum name
(Enum.name()
) and converts name to Enum
by searching Class.getEnumConstants()
.
Column should be a sql type that is compatible with java String (like varchar) and wide enough for the longest Enum name.
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()
.
Subclass to create a custom TypeTranslator
for Enum fields. Override
read(ResultSet, int)
and write(PreparedStatement, int, Enum)
.
Constructor and Description |
---|
EnumTranslator() |
Modifier and Type | Method and Description |
---|---|
T |
getDefaultEnum()
Gets the default Enum associated with
getDefaultEnumName() . |
java.lang.String |
getDefaultEnumName()
Gets the Enum name to use in
read(ResultSet, int) when the name read from
database cannot be found in Class.getEnumConstants() of class getEnumClass() . |
java.lang.Class<T> |
getEnumClass()
Gets the class to use to look up Enum by name using
Class.getEnumConstants() . |
java.lang.Enum<T> |
read(java.sql.ResultSet resultSet,
int columnIndex)
Reads the name of the Enum from the database and finds the corresponding Enum from
Class.getEnumConstants() . |
void |
setDefaultEnumName(java.lang.String defaultEnumName)
Sets the Enum name to use in
read(ResultSet, int) when the Enum name read
from the database is not a valid Enum name. |
void |
setEnumClass(java.lang.Class<T> enumClass)
Sets the class to use to look up Enum by name using
Class.getEnumConstants() . |
void |
write(java.sql.PreparedStatement preparedStatement,
int parameterIndex,
java.lang.Enum<T> parameter)
Writes the name of the Enum parameter to database as a String.
|
public java.lang.Class<T> getEnumClass()
Class.getEnumConstants()
.public void setEnumClass(java.lang.Class<T> enumClass)
Class.getEnumConstants()
.
This method is typically invoked by RowTranslator
to initialize this translator
when Table
object is initialized that contains an Enum fieldenumClass
- Class of Enum to be translatedpublic java.lang.String getDefaultEnumName()
read(ResultSet, int)
when the name read from
database cannot be found in Class.getEnumConstants()
of class getEnumClass()
.public void setDefaultEnumName(java.lang.String defaultEnumName)
read(ResultSet, int)
when the Enum name read
from the database is not a valid Enum name. If no default has been set then
read(ResultSet, int)
will return null.
setEnumClass(Class)
must be invoked prior to invoking this method. This method is
invoked by RowTranslator
when Table
object is initialized.
defaultEnumName
- default name of Enum to use or empty string for no defaultpublic T getDefaultEnum()
getDefaultEnumName()
.public void write(java.sql.PreparedStatement preparedStatement, int parameterIndex, java.lang.Enum<T> parameter) throws java.lang.Exception
write
in interface TypeTranslator<java.lang.Enum<T extends java.lang.Enum<T>>>
preparedStatement
- set column value as parameter in this statementparameterIndex
- set parameter at this indexparameter
- value to set in prepared statementjava.lang.Exception
- if errorpublic java.lang.Enum<T> read(java.sql.ResultSet resultSet, int columnIndex) throws java.lang.Exception
Class.getEnumConstants()
. If column is null then null is returned. If no Enum
can be found, then Enum with name of getDefaultEnumName()
is returned.
Reads value from result set.read
in interface TypeTranslator<java.lang.Enum<T extends java.lang.Enum<T>>>
resultSet
- read value from this result setcolumnIndex
- read value at this column index from result setjava.lang.Exception
- if error