本文介绍了使用枚举属性作为ormlite列值,而不是序号。可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

是否有可能使用tipoValue枚举属性作为值的列代替序号

Is it possible to use the tipoValue enum property as the value for the column instead of the ordinal?

编辑:这里就是我所做的:

here's what i did:

创建了MyEnumPersister类:

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

}

}

在javatoSqlArg我直接返回通过使用枚举内的吸气剂对应于estadoValue字符串。
我想从数据库中,我将需要实现s​​qlArgToJava方法太拿回来一个枚举,但我还没有建立它。

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:看来,当我做保存数据,方法javaToSqlArg得到的sqlArgToJava之前调用表的查询,为什么?这里的查询:

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

obj的值显示为这是我转换成SQL时我在数据库中插入为P。犯规什么太大的意义......

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....

推荐答案

默认情况下ORMLite存储枚举类型的字符串名称 - 在你的情况下,将存储 REGISTARNOTIFICACAORESPOSTA 。您可以覆盖此行为,并通过 ENUM_INTEGER 数据类型存储序号值:

By default ORMLite stores the string name of the enumerated type -- in your case it would store REGISTARNOTIFICACAORESPOSTA. You can override this behavior and store the ordinal value by using the ENUM_INTEGER data-type:

@DatabaseField(dataType=DataType.ENUM_INTEGER)
private TipoPedido tipo;

这将存储整数数据库而不是字符串序号值。引用文档:

This will store the integer ordinal value in the database instead of the string. To quote the docs:

字符串名称是默认(和建议在 ENUM_INTEGER ),因为它可以让你在列表中的任意位置添加额外的枚举,而不必担心数据后进行转换。

不过,好像你要存储从枚举的 tipoValue 字符串而不是。要做到这一点,你正在做的有,而不是注册自定义持留。查看有关此文档:

However, it seems like you want to store the tipoValue string from the enum instead. To do this you are doing to have to register a custom persister instead. See the docs about this:

这让你想如何在Java领域转化为数据库重新presentation您具体界定,反之亦然。这两种重要的方法是:

This allows you to define specifically how you want to translate the Java field into the database representation, and vice versa. The two important methods are:


  • 它从Java转换成数据库格式和

  • 从数据库转换回Java的。

您可以延长并建立从地面转换器或可以扩展的或的,只是覆盖上面的重要方法。

You can extend BaseDataType and build the converter from the ground up or you can extend EnumStringType or StringType and just override the above important methods.

这篇关于使用枚举属性作为ormlite列值,而不是序号。可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:31