How to get ForeignCollection Field into Cursor in Ormlite

怎甘沉沦 提交于 2019-12-08 05:22:45

问题


I have a ForeignCollection field in my table below :

@DatabaseTable(tableName = "json_main")
public class JsonMain {

    @DatabaseField( id = true)
    int _id;

    @DatabaseField
    int likes_count;

    @DatabaseField 
    String protected_visibility;

    @ForeignCollectionField
    ForeignCollection<JsonUser>    jsonUser = null;
}

And Reference of this table here :

@DatabaseTable (tableName = "json_main_user")
public class JsonUser {

    @DatabaseField(generatedId = true)
    public int _id;

    @DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = "parent_id")
    private JsonMain jsonMain;
}

I'm Building My Query to get The cursor here :

    QueryBuilder<JsonMain, Integer> queryBuilder_main = dao_json_main.queryBuilder();
    PreparedQuery<JsonMain> jsonMains_list =queryBuilder_main.orderBy("sort_id",false).prepare();
    CloseableIterator<JsonMain> closeableIterator = dao_json_main.iterator(jsonMains_list);

    AndroidDatabaseResults androidDatabaseResults=    (AndroidDatabaseResults)closeableIterator.getRawResults();

    Cursor cursor = androidDatabaseResults.getRawCursor();

I have inserted the Data Correctly into Database I want to Know How could I get ForeignCollection of json_main table using ormlite using Cursor .Above is what I have tried .

Any Answer or Suggestion is Highly Appreciated


回答1:


I have inserted the Data Correctly into Database I want to Know How could I get ForeignCollection of json_main table using ormlite using Cursor .Above is what I have tried .

I hope I understand the question. The JsonMain Cursor doesn't have any information from the JsonUser table. What it does have is the _id field which corresponds to the jsonMain_id field which is in the JsonUser table. What you can do is take that _id and do a query on the JsonUser table to get the associated users.

The only other way to do this would be to construct your own JOIN raw query which returns both the main fields and the user fields in one cursor. Unfortunately ORMLite won't help much in this scenario.



来源:https://stackoverflow.com/questions/22071367/how-to-get-foreigncollection-field-into-cursor-in-ormlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!