java - Is it possible to have an enum field in a class persisted with OrmLite? -


I am trying to continue with the following class:

  public Square field {@ DatabaseField (id = true) name of the public string; @DatabaseField (canBeNull = false) Public field type type; ...}  

field type is a public enum . The field corresponds to the type , string in SQLite (does not support embems). When I try to use it, I get the following exception:

  information [main] (SingleConnectionDataSource.java:244) - Install shared JDBC connection: org.sqlite.Conn@5224ee Exception in the formula "main" org.springframework.beans.factory.BeanInitializationException: DAO initially failed; Area enums.FieldType uncharted territory class to FieldType: name = type, class = org.springframework.dao.support.DaoSupport.afterPropertiesSet (DaoSupport.java:51) is nested exception java.lang.IllegalArgumentException class orm.Field JavaklangkIllegalArgumentException :: due to unknown area square orm.FieldDAO.getInstance orm.Field.fromString (Field.java:23) to (FieldDAO.java:17) on orm.Field.main (Field.java:38) From Field to FieldType class enums. FieldType: com.j256.ormlite.field.FieldType name = type, square = class orm.Field & lt; Init & gt; Com.j256.ormlite.field.FieldType (FieldType.java:54). .createFieldType (FieldType.java:381) organization on com.j256.ormlite.table.DatabaseTableConfig.fromClass (DatabaseTableConfig.java:82) on com.j256.ormlite.dao.BaseJdbcDao.initDao (BaseJdbcDao.java:116). springframework.dao.support.DaoSupport.afterPropertiesSet (DaoSupport.java:48) ... 3 more  

From then I tell how OrmLite, Java side values ​​a Enum ?

Enums can continue as either:

  // This saves it as a string database @ database field OurAssuming ours; ... personal reward OurEnum {FIRST, SECOND ,; }  

As an alternative, you can save.

  // saves it as an integer in database @ databasefilld (dataType = DataType.ENUM_INTEGER). Our Inman is our Annam;  

Although you can store in sequence, then the version of the name VARCHAR (which is the default) is recommended because Sequential values ​​can change if you add or remove entries from enum.

For both anonymous types, you can specify which helps with forward and backward compatibility. If there is an unknown value for the enum in the database, then the objects are returned by the DAO, then it will have an enum value.

  @DatabaseField (unknownEnumName = "FIRST") OurEnum ourEnum;  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -