Using enum property as ormlite column value instead of ordinal. is it possible?

落花浮王杯 提交于 2019-12-06 16:10:15

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:

The string name is the default (and recommended over ENUM_INTEGER) because it allows you to add additional enums anywhere in the list without worrying about having to convert data later.

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:

http://ormlite.com/docs/custom-persister

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:

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.

jsmith

I use the Enum.name() method to fill in Db columns and Enum.valueOf() to parse the value back. IMHO, I think the oridinals should never be used.

Also, you might want to take a look at this with regards to storing labels for enumeration values:

Is there an Enum string resource lookup pattern for Android?strong text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!