gpt4 book ai didi

android - ORMLITE 一对多递归关系

转载 作者:行者123 更新时间:2023-11-30 02:36:09 25 4
gpt4 key购买 nike

你好,我遇到了一个问题:

我需要存储主题的树形表示。

我的意思是一个 SUBJECT 有一个 SUBJECT 类型的 child 列表

 @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 存储树结构???

最佳答案

尝试将儿子类型更改为 ForeignCollection 类型

@ForeignCollectionField
private ForeignCollection<Subject> sons;

关于android - ORMLITE 一对多递归关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26497117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com