@DatabaseField(dataType=DataType.ENUM_STRING,columnName=TIPO_FIELD_NAME)
private TipoPedido tipo;
public enum TipoPedido {
REGISTARNOTIFICACAORESPOSTA("Registar Notificação Resposta",SIGLA_TIPO_REGISTARNOTIFICATIORESPOSTA);
private String tipoName;
private String tipoValue;
TipoPedido(String tipoName,String tipoValue){
this.tipoName=tipoName;
this.tipoValue=tipoValue;
}
public String getTipoName(){
return tipoName;
}
public String getTipoValue(){
return tipoValue;
}
}
Is it possible to use the tipoValue enum property as the value for the column instead of the ordinal?
Edit:here's what i did:
Created the MyEnumPersister class:
public class MyEnumPersister extends EnumStringType{
private static final MyEnumPersister singleTon = new MyEnumPersister();
/**
* @param sqlType
* @param classes
*/
protected MyEnumPersister() {
super(SqlType.STRING, new Class<?>[] { Enum.class });
}
public static MyEnumPersister getSingleton() {
return singleTon;
}
@Override
public Object javaToSqlArg(FieldType fieldType, Object obj) {
return ((EstadoPedido)obj).getEstadoValue();
}
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)
throws SQLException {
return PedidoDTO.siglaEstadoPedidoComparator((String)sqlArg);
}
}
In javatoSqlArg i return directly the string corresponding to the estadoValue by using the getter inside the Enum. I assume to get it back to a Enum from the DataBase i will need to implement the sqlArgToJava method too, but i'm yet to build it.
edit2: Seems that when i do a query on the table that holds the data, the method javaToSqlArg gets called before the sqlArgToJava, why??? here's the query:
public List<PedidoDTO> getPendingRequests(){
List<PedidoDTO> pendingReq=null;
QueryBuilder<PedidoDTO, Integer> queryBuild=null;
try {
queryBuild=getHelper().getPedidosDao().queryBuilder();
pendingReq=queryBuild.where().eq(PedidoDTO.ESTADO_FIELD_NAME, PedidoDTO.SIGLA_ESTADO_PENDENTE).query();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pendingReq;
}
The obj value appears as "P" which was what i convert to sql when i inserted it in the Database. Doesnt make too much sense....