I'm trying to save enum collection in database with ORMLite. So while creating Dao I'm getting no-args constructor error
I have tried create default package contructor, but it doesn't helps - I think ORMLite requires public constructor, but enums cannot have it.
My enum looks like this (separated .java file)
enum RankPermission {
@DatabaseField(dataType=DataType.ENUM_TO_STRING)
ALL,
//...
;
RankPermission() {}
}
And collection in another class is
@DatabaseTable
class Rank {
@ForeignCollectionField
Collection<RankPermission> permissions;
public Rank() {}
}
Dao creation and error which I have got
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) ~[?:?]
I would like to save this collection in enum form, beacause it is more comfortable
To solve this problem I've just used any Collection from java.util
package and set @DatabaseField(dataType=DataType.SERIALIZABLE)
annotation instead of @ForeignCollectionField
to save serialized data, so now code is
class Rank {
@DatabaseField(dataType=DataType.SERIALIZABLE)
private HashSet<RankPermission> permissions = new HashSet<>();
}
It's important to set HashSet
type to field, because ORMLite will not detect serializable interface on Set
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.