クラスがあります
@DatabaseTable(tableName = "subjects")
public class Subject {
private static final String SUBJECT_ID = "subject_id";
@SerializedName("subject_id")
@DatabaseField(columnName = SUBJECT_ID,id = true,generatedId = false)
int id;
@SerializedName("title")
@DatabaseField(dataType = DataType.STRING)
String title;
public Subject()
{}
public int getSubjectId()
{
return this.id;
}
public String getSubjectTitle()
{
return this.title;
}
public void setSubjectId(int id)
{
this.id = id;
}
public void setSubjectTitle(String title)
{
this.title = title;
}
@Override
public String toString()
{
return "{title="+title +" "+"subject_id"+id+"}";
}
}
クラスで「多対1」を参照しています:
@DatabaseTable(tableName = "categories")
public class Category {
public final static String CATEGORY_TITLE_FIELD_NAME = "title";
public final static String SUBJECT_TITLE_FIELD_NAME = "subject";
private static final String SUBJECT_ID = "subject_id";
@SerializedName("id")
@DatabaseField(id = true, generatedId = false)
int id;
@SerializedName("title")
@DatabaseField(dataType = DataType.STRING, columnName = CATEGORY_TITLE_FIELD_NAME)
String title;
@SerializedName("subject")
@DatabaseField(columnName = SUBJECT_ID,foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
Subject subject;
public Category() {
}
public Category(int id, String title, Subject subject) {
this.id = id;
this.title = title;
this.subject = subject;
}
public int getCategoryId() {
return this.id;
}
public String getCategoryTitle() {
return this.title;
}
public Subject getCategorySubject() {
return this.subject;
}
public void setCategoryId(int id) {
this.id = id;
}
public void setCategoryTitle(String title) {
this.title = title;
}
public void setCategorySubject(Subject subject) {
this.subject = subject;
}
@Override
public String toString() {
return "{title=" + title + " " + "id=" + id + "subject" + subject+ "}";
}
}
そして、最初にアクティビティでサブジェクトを追加し、次にカテゴリを追加します。参照を使用しない場合、すべてが正常に機能します。Subject オブジェクトを外部キーとして作成したい場合、次のようになります。
08-21 18:19:24.369: W/System.err(4049): java.sql.SQLException: Unknown field 'subject_id' from the Android sqlite cursor, not in:[title, id]
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.android.AndroidDatabaseResults.findColumn(AndroidDatabaseResults.java:97)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:793)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:60)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:161)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:261)
08-21 18:19:24.369: W/System.err(4049): at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:262)
08-21 18:19:24.369: W/System.err(4049): at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:276)
08-21 18:19:24.369: W/System.err(4049): at com.assignmentexpert.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
08-21 18:19:24.369: W/System.err(4049): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-21 18:19:24.369: W/System.err(4049): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-21 18:19:24.369: W/System.err(4049): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-21 18:19:24.369: W/System.err(4049): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-21 18:19:24.369: W/System.err(4049): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-21 18:19:24.369: W/System.err(4049): at java.lang.Thread.run(Thread.java:1019)
08-21 18:19:24.419: I/ApplicationPackageManager(4049): cscCountry is not German : SEK
08-21 18:19:24.549: D/AndroidRuntime(4049): Shutting down VM
08-21 18:19:24.549: W/dalvikvm(4049): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-21 18:19:24.609: E/AndroidRuntime(4049): FATAL EXCEPTION: main
08-21 18:19:24.609: E/AndroidRuntime(4049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.DashboardActivity}: java.lang.NullPointerException
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.os.Looper.loop(Looper.java:123)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-21 18:19:24.609: E/AndroidRuntime(4049): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 18:19:24.609: E/AndroidRuntime(4049): at java.lang.reflect.Method.invoke(Method.java:507)
08-21 18:19:24.609: E/AndroidRuntime(4049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-21 18:19:24.609: E/AndroidRuntime(4049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-21 18:19:24.609: E/AndroidRuntime(4049): at dalvik.system.NativeStart.main(Native Method)
08-21 18:19:24.609: E/AndroidRuntime(4049): Caused by: java.lang.NullPointerException
08-21 18:19:24.609: E/AndroidRuntime(4049): at com.assignmentexpert.DashboardActivity.onCreate(DashboardActivity.java:66)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-21 18:19:24.609: E/AndroidRuntime(4049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-21 18:19:24.609: E/AndroidRuntime(4049): ... 11 more