I am parsing a JSON file to generate rows and relationships in a database with OrmLite. Using the following code, it takes about 20+ minutes to parse all of the content. Is there any way I can optimize this to take less time?
I have 3 tables making up a many-to-many relationship.
public class FirstTable {
int id;
ForeignCollection<IntermediateTable> intermediateTables;
}
public class IntermediateTable {
int id;
FirstTable firstTable;
SecondTable secondTable;
}
public class SecondTable {
int id;
ForeignCollection<IntermediateTable> intermediateTables;
}
After creating and populating the first and second tables, I am parsing a JSON file to create the relations between the FirstTable and SecondTable. The JSON file stores a collection of FirstTable objects and the IDs of the relating SecondTable entries.
My code looks something like this:
setForeignRelations(JSONObject jsonObject, FirstTable firstTable) {
JSONArray secondTables = jsonObject.getJSONArray(SECOND_TABLE_KEY);
for (int i = 0; i < secondTables.length(); i++) {
int secondTableId = ((Integer)secondTables.get(i)).intValue();
SecondTable secondTable = DbManager.getInstance().getHelper().getSecondTableDao().queryForId(secondTableId);
IntermediateTable intermediateTable = new IntermediateTable();
intermediateTable.setFirstTable(firstTable);
intermediateTable.setSecondTable(secondTable);
DbManager.getInstance().getHelper().getIntermediateTableDao().create(intermediateTable);
}
}