ORMLITE one-to-many recursive relationship

2019-08-02 19:24发布

Hello i am facing a problem:

I need to store a tree representation of subjects.

I mean a SUBJECT have a list of childrens of type SUBJECT

 @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;

this code throws an error

 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)

can anybody show me how to store a tree structure using ormlite???

1条回答
小情绪 Triste *
2楼-- · 2019-08-02 20:10

Try changing the sons type to type of ForeignCollection

@ForeignCollectionField
private ForeignCollection<Subject> sons;
查看更多
登录 后发表回答