我正在尝试使用ORMLite将枚举集合保存在数据库中。因此,在创建Dao时,我遇到了无参数构造函数错误
我曾尝试创建默认的包构造器,但这无济于事-我认为ORMLite需要公共构造函数,但枚举不能拥有它。
我的枚举看起来像这样(分离的.java文件)
enum RankPermission {
@DatabaseField(dataType=DataType.ENUM_TO_STRING)
ALL,
//...
;
RankPermission() {}
}
另一个类中的集合是
@DatabaseTable
class Rank {
@ForeignCollectionField
Collection<RankPermission> permissions;
public Rank() {}
}
我的道创造与错误
DaoManager.createDao(conn, Rank.class);
[23:31:12 ERROR]: Can't find a no-arg constructor for class package.core.rank.RankPermission
java.lang.IllegalArgumentException: Can't find a no-arg constructor for class package.core.rank.RankPermission
at com.j256.ormlite.table.DatabaseTableConfig.findNoArgConstructor(DatabaseTableConfig.java:229) ~[?:?]
at com.j256.ormlite.table.DatabaseTableConfig.getConstructor(DatabaseTableConfig.java:154) ~[?:?]
at com.j256.ormlite.table.TableInfo.<init>(TableInfo.java:96) ~[?:?]
at com.j256.ormlite.table.TableInfo.<init>(TableInfo.java:54) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:156) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:133) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:112) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:1044) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:1044) ~[?:?]
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:70) ~[?:?]
at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:392) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:206) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:133) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:112) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:1044) ~[?:?]
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:1044) ~[?:?]
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:70) ~[?:?]
at package.storage.yaml.db.DatabaseConnector.initDaos(DatabaseConnector.java:152) ~[?:?]
我想将此集合保存为枚举形式,因为它更舒适
答案 0 :(得分:1)
要解决此问题,我只使用了java.util
包中的任何Collection并设置了@DatabaseField(dataType=DataType.SERIALIZABLE)
注释而不是@ForeignCollectionField
来保存序列化数据,所以现在的代码是
class Rank {
@DatabaseField(dataType=DataType.SERIALIZABLE)
private HashSet<RankPermission> permissions = new HashSet<>();
}
将HashSet
类型设置为字段很重要,因为ORMLite不会检测Set
上的可序列化接口。