ORMLITE一个一对多递归关系(ORMLITE one-to-many recursive rel

2019-10-21 04:25发布

你好,我面临的一个问题:

我需要存储的对象树表示。

我的意思是受试者是否有对象类型的儿童名单

 @DatabaseTable(tableName = "subject")
public class Subject implements Serializable {

@DatabaseField(columnName = "_id")
private int id;
@DatabaseField
private String text;

@DatabaseField(columnName = "parent", foreign = true)
private Subject parent;

@ForeignCollectionField
private List<Subject> sons;

此代码抛出错误

 9950-9950/ec.com.smx.flux E/EC.COM.SMX.FLUX/SubjectDaoProvider.java:25﹕ main java.sql.SQLException: Field class for 'sons' must be of class ForeignCollection or Collection.
            at com.j256.ormlite.field.FieldType.<init>(FieldType.java:167)
            at com.j256.ormlite.table.DatabaseTableConfig.convertFieldConfigs(DatabaseTableConfig.java:236)
            at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:101)
            at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:151)
            at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:126)
            at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:117)
            at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:919)
            at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:919)
            at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:72)
            at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.getDao(OrmLiteSqliteOpenHelper.java:277)
            at ec.com.smx.flux.data.DatabaseHelperImpl.getSubjectDao(DatabaseHelperImpl.java:372)
            at ec.com.smx.flux.data.conf.SubjectDaoProvider.get(SubjectDaoProvider.java:23)
            at ec.com.smx.flux.data.conf.SubjectDaoProvider.get(SubjectDaoProvider.java:14)
            at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:86)
            at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:73)
            at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:66)
            at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:63)
            at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
            at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1058)
            at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
            at com.google.inject.Scopes$1$1.get(Scopes.java:65)
            at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
            at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:205)
            at com.google.inject.internal.InternalInjectorCreator$1.call(InternalInjectorCreator.java:199)
            at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1051)
            at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:199)
            at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:180)
            at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
            at com.google.inject.Guice.createInjector(Guice.java:96)
            at com.google.inject.Guice.createInjector(Guice.java:84)
            at roboguice.RoboGuice.setBaseApplicationInjector(RoboGuice.java:94)
            at ec.com.smx.flux.FluxApplication.onCreate(FluxApplication.java:53)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4581)
            at android.app.ActivityThread.access$1600(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

有谁能够告诉我如何使用ormlite存储树结构???

Answer 1:

尝试改变儿子键入类型ForeignCollection的

@ForeignCollectionField
private ForeignCollection<Subject> sons;


文章来源: ORMLITE one-to-many recursive relationship