是否可以使用 OrmLite 保留类中的枚举字段?

发布于 2024-09-05 17:18:30 字数 1591 浏览 1 评论 0 原文

我正在尝试使用 OrmLite 保留以下类:

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

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

FieldType 是 <代码>公共枚举。 SQLite 中与 type 对应的字段是字符串(不支持枚举)。当我尝试使用它时,出现以下异常:

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

那么如何告诉 OrmLite,Java 端的值来自 enum

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2024-09-12 17:18:30

ORMLite 可以将枚举持久化为 VARCHAR 枚举名称(默认)

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

作为替代方案,您可以保存 序号INTEGER

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

尽管您可以存储序数,但建议使用VARCHAR名称版本(这是默认值),因为如果您在枚举中添加或删除条目,序数值可能会发生变化。

对于这两种枚举类型,您可以指定 unknownEnumName = "..." 字段< /a> 这有助于向前和向后兼容性。如果数据库包含未知的枚举值,则 DAO 返回的对象将具有该枚举值。

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文