How to get ForeignCollection Field into Cursor in

2019-05-30 07:42发布

问题:

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.