在ORMLite中使用外键

时间:2012-08-21 15:32:43

标签: android foreign-keys foreign-key-relationship ormlite

我有一个班级

   @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+"}";    
    }

}

与类别一起引用“多对一”:

    @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

1 个答案:

答案 0 :(得分:0)

删除应用程序并使用上述主题和类别重新安装它。据我所知,当数据库已经创建时,在应用程序开发过程中在Subject类中添加columnName = SUBJECT_ID时可能会发生此错误。