Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8441645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:37:00+00:00 2026-06-10T08:37:00+00:00

So i posted this question, Using enum property as ormlite column value instead of

  • 0

So i posted this question, Using enum property as ormlite column value instead of ordinal. is it possible?, about how to use anum property value as the value passed to the database instead of the name, and it worked. I created the persistclass with the methods javaToSqlArg( for inserting in the Database) and the sqlArgoToJava for retrieving data from the database to the objects.

The problem seems that when doing the latter. The javaToSqlArg method is called before the sqlArgToJava which was the one I expected to be called directly when making select queries. Once again here is the persist class:

public class MyEnumPersister extends EnumStringType{

    private static final MyEnumPersister singleTon = new MyEnumPersister();

    protected MyEnumPersister() {
        super(SqlType.STRING, new Class<?>[] { Enum.class });
    }

    public static MyEnumPersister getSingleton() {
        return singleTon;
    }

    @Override
    public Object javaToSqlArg(FieldType fieldType, Object obj) {
        if(obj instanceof EstadoPedido){
            return ((EstadoPedido)obj).getEstadoValue();
        }else if(obj instanceof TipoPedido){
            return ((TipoPedido)obj).getTipoValue();
        }
        return obj;
    }               

    @Override
    public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
        if(fieldType.getColumnName().equals(PedidoDTO.ESTADO_FIELD_NAME)){  
            return PedidoDTO.siglaEstadoPedidoComparator((String)sqlArg);
        }else{
            return PedidoDTO.siglaTipoPedidoComparator((String)sqlArg);
        }
    }
}

And the dto class:

public class PedidoDTO extends IpdmsMobileDTO {

    private static final long serialVersionUID = 268620648774735484L;

    public final static String SIGLA_ESTADO_ENVIADO="E";
    public final static String SIGLA_ESTADO_PENDENTE="P";
    public final static String SIGLA_ESTADO_CANCELADO="C";
    public final static String SIGLA_ESTADO_CANCELADOALTERADO="CAP";

    public final static String SIGLA_TIPO_REGISTARNOTIFICATIORESPOSTA="RNR";

    //ormlime column names
    public final static String ESTADO_FIELD_NAME = "estado";
    public final static String TIPO_FIELD_NAME = "tipo";
    public final static String DATAENVIO_FIELD_NAME = "data_envio";
    public final static String ENTIDADE_ID_FIELD_NAME = "entidade_id";

    @DatabaseField(columnName=ESTADO_FIELD_NAME, persisterClass=MyEnumPersister.class)
    private EstadoPedido estado;
    @DatabaseField(columnName=TIPO_FIELD_NAME,  persisterClass=MyEnumPersister.class)
    private TipoPedido tipo;
    @DatabaseField(columnName=DATAENVIO_FIELD_NAME)
    private Date dataEnvio;
    @DatabaseField(columnName=ENTIDADE_ID_FIELD_NAME)
    private int idEntidade;

    public PedidoDTO() {
        //for ormlite
    }   

    public EstadoPedido getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado=siglaEstadoPedidoComparator(estado);
    }

    public static EstadoPedido siglaEstadoPedidoComparator(String estado){
        if (estado.equals(SIGLA_ESTADO_PENDENTE))
            return EstadoPedido.PENDENTE;
        else if (estado.equals(SIGLA_ESTADO_ENVIADO))
            return EstadoPedido.ENVIADO;
        else if(estado.equals(SIGLA_ESTADO_CANCELADO))
            return EstadoPedido.CANCELADO;
        else
            return EstadoPedido.CANCELADOALTERADOPREV;
    }

    public TipoPedido getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        //set tipo do pedido
        this.tipo =siglaTipoPedidoComparator(tipo);
    }

    public static TipoPedido siglaTipoPedidoComparator(String tipo){
        return TipoPedido.REGISTARNOTIFICACAORESPOSTA;
    }

    public enum EstadoPedido{
        ENVIADO  ("Enviado"  ,SIGLA_ESTADO_ENVIADO), 
        PENDENTE ("Pendente" ,SIGLA_ESTADO_PENDENTE),
        CANCELADO("Cancelado",SIGLA_ESTADO_CANCELADO),
        CANCELADOALTERADOPREV("Cancelado/Alterado Previamente",SIGLA_ESTADO_CANCELADO);

        private String estadoName;  
        private String estadoValue;

        EstadoPedido(String estadoName,String estadoValue){
            this.estadoName=estadoName;
            this.estadoValue=estadoValue;
        }

        public String getEstadoName(){
            return estadoName;
        }

        public String getEstadoValue(){
            return estadoValue;
        }
    }

    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;
        }
    }
}

and 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:37:02+00:00Added an answer on June 10, 2026 at 8:37 am

    The javaToSqlArg method is called before the sqlArgToJava which was the one I expected to be called directly when making select queries. Once again here is the persist class:

    ORMLite tries to be consistant about this @Max. You are passing in a Java value to the eq comparison method and it tries to perform the same conversion on it that happens when you store it to the database. In this case, since the Java field is a EstadoPedido or TipoPedido, the value you pass to the eq method should be that as well.

    So your QueryBuilder eq call should use an enum value not a String and should be changed to something like:

    where().eq(PedidoDTO.ESTADO_FIELD_NAME, EstadoPedido.PENDENTE)
    

    I think I got that right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I posted this question regarding using lambdas inside of a Join() method to
I am using the BlockingQueue code posted in this question , but realized I
A few days ago, I posted this question and this question asking about how
Note: I accidentally posted this question without specifying which STL implementation I was using,
Last Updated: 2009-08-11 2:30pm EDT A few days ago I posted this question about
Note: I've cross-posted this question in the grails-user mailing list This weekend using this
I originally posted this question looking for an answer with using python, got some
A few days ago I posted this question: switch statement and loops using jquery/javascript
I recently posted this question about codes for a gift-card-like voucher that users can
A day or so ago I posted this question: Confused about enumeration... How do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.