Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to persist the following class with OrmLite:

public class Field {
    @DatabaseField(id = true)
    public String name;

    @DatabaseField(canBeNull = false)
    public FieldType type;
    ...
}

The FieldType is a public enum. The field, corresponding to the type is string in SQLite (is doesn't support enums). When I try to use it, I get the following exception:

INFO [main] (SingleConnectionDataSource.java:244) - Established shared JDBC Connection: org.sqlite.Conn@5224ee
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Initialization of DAO failed; nested exception is java.lang.IllegalArgumentException: Unknown field class class enums.FieldType for field FieldType:name=type,class=class orm.Field
 at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:51)
 at orm.FieldDAO.getInstance(FieldDAO.java:17)
 at orm.Field.fromString(Field.java:23)
 at orm.Field.main(Field.java:38)
Caused by: java.lang.IllegalArgumentException: Unknown field class class enums.FieldType for field FieldType:name=type,class=class orm.Field
 at com.j256.ormlite.field.FieldType.<init>(FieldType.java:54)
 at com.j256.ormlite.field.FieldType.createFieldType(FieldType.java:381)
 at com.j256.ormlite.table.DatabaseTableConfig.fromClass(DatabaseTableConfig.java:82)
 at com.j256.ormlite.dao.BaseJdbcDao.initDao(BaseJdbcDao.java:116)
 at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:48)
 ... 3 more

So how do I tell OrmLite, values on the Java side are from an enum?

like image 252
htf Avatar asked Jun 15 '10 11:06

htf


People also ask

Can enums be subclassed?

We've learned that we can't create a subclass of an existing enum. However, an interface is extensible. Therefore, we can emulate extensible enums by implementing an interface.

What is an enum used for in programming Why would we want to use enumerations?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Can enums be changed?

4) Enum constants are implicitly static and final and can not be changed once created.

Can enums contain methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.


1 Answers

ORMLite can persist enums either as the VARCHAR enum name (default):

// this saves it as a string in the database
@DatabaseField
OurEnum ourEnum;
...
private enum OurEnum {
    FIRST,
    SECOND, ;
}

As an alternative, you can save the ordinal INTEGER.

// this saves it as an integer in the database
@DatabaseField(dataType = DataType.ENUM_INTEGER)
OurEnum ourEnum;

Although you can store the ordinal, the VARCHAR name version (which is the default) is recommended since the ordinal value can change if you add or remove entries from the enum.

For both enum types, you can specify an unknownEnumName = "..." field which helps with forward and backward compatibility. If the database contains an unknown value for the enum then the object that is returned by the DAOs will have this enum value.

@DatabaseField(unknownEnumName = "FIRST")
OurEnum ourEnum;
like image 69
Gray Avatar answered Oct 01 '22 20:10

Gray