我在我的android项目中使用ormlite。我想让外键工作。 这就是我现在所拥有的。
我在以下结构中获取json,我需要将其保存到数据库中。为此,我制作了以下对象类。
我得到的第一堂课是:
@DatabaseTable(tableName = "gallery")
public class Gallery extends DatabaseModel {
public static final String FIELD_ID = "gal_id";
public static final String FIELD_TITLE = "gal_title";
public static final String FIELD_IMAGES = "gal_images";
//ID
@DatabaseField(columnName = FIELD_ID, id = true)
@SerializedName(FIELD_ID)
private int id;
//FIELDS
@DatabaseField(columnName = FIELD_TITLE)
@SerializedName(FIELD_TITLE)
private String title;
@ForeignCollectionField()
@SerializedName(FIELD_IMAGES)
private ForeignCollection<GalleryImage> images;
public Gallery() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String published) {
this.title = title;
}
public ForeignCollection<GalleryImage> getImages() {
return images;
}
public void setImages(ForeignCollection<GalleryImage> images) {
this.images = images;
}
@Override
public void SaveAll(DataRepository repo, List<? extends DatabaseModel> models) {
repo.galleryRepository.deleteAll();
repo.galleryRepository.saveAll(repo, models);
}
@Override
public void Save(DataRepository repo) {
repo.galleryRepository.create(this);
}
}
第二节课是:
@DatabaseTable(tableName = "galleryimages")
public class GalleryImage {
public static final String FIELD_ID = "pho_id";
public static final String FIELD_URL = "pho_url";
public static final String FIELD_GALLERY_ID = "pho_gal_id";
//ID
@DatabaseField(columnName = FIELD_ID, id = true)
@SerializedName(FIELD_ID)
private int id;
//FIELDS
@DatabaseField(columnName = FIELD_URL)
@SerializedName(FIELD_URL)
private String url;
@DatabaseField(columnName = FIELD_GALLERY_ID)
@SerializedName(FIELD_GALLERY_ID)
private int galleryId;
public GalleryImage() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String published) {
this.url = url;
}
public int getGalleryId() {
return galleryId;
}
public void setGalleryId(int galleryId) {
this.galleryId = galleryId;
}
}
正如您所看到的,ForeingCollection有多个GalleryImage。在GalleryImage类中,我有一个名为:“pho_gal_id”的Id,它链接回“gal_id”字段。
这是我运行这两个类时得到的例外:
05-23 09:46:57.040 4613-4613/be.appmax.twentebad.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: be.appmax.twentebad.app, PID: 4613
java.lang.RuntimeException: Unable to start activity ComponentInfo{be.appmax.twentebad.app/be.appmax.twentebad.app.MainActivity}: java.lang.RuntimeException: java.sql.SQLException: Foreign collection class be.appmax.twentebad.app.models.database.GalleryImage for field 'images' column-name does not contain a foreign field of class be.appmax.twentebad.app.models.database.Gallery
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5144)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.sql.SQLException: Foreign collection class be.appmax.twentebad.app.models.database.GalleryImage for field 'images' column-name does not contain a foreign field of class be.appmax.twentebad.app.models.database.Gallery
at be.appmax.twentebad.app.database.DatabaseHelper.onCreate(DatabaseHelper.java:70)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:209)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:66)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:54)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:243)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:196)
at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:118)
at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:241)
at be.appmax.twentebad.app.database.repositories.MenuRepository.getAll(MenuRepository.java:87)
at be.appmax.twentebad.app.MainActivity.readDatabase(MainActivity.java:103)
at be.appmax.twentebad.app.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5144)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.sql.SQLException: Foreign collection class be.appmax.twentebad.app.models.database.GalleryImage for field 'images' column-name does not contain a foreign field of class be.appmax.twentebad.app.models.database.Gallery
at com.j256.ormlite.field.FieldType.findForeignFieldType(FieldType.java:1020)
at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:390)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:201)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.DaoManager.doCreateDao(DaoManager.java:359)
at com.j256.ormlite.dao.DaoManager.createDaoFromConfig(DaoManager.java:326)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:55)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:218)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53)
at be.appmax.twentebad.app.database.DatabaseHelper.onCreate(DatabaseHelper.java:65)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:209)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:66)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:54)
at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:243)
at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:196)
at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:118)
at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:241)
at be.appmax.twentebad.app.database.repositories.MenuRepository.getAll(MenuRepository.java:87)
at be.appmax.twentebad.app.MainActivity.readDatabase(MainActivity.java:103)
at be.appmax.twentebad.app.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5144)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
我通过自己进行继承并将它们保存到两个不同的数据库来解决问题。