To automatically fetch a referenced object upon querying you can use foreignAutoRefresh = true
in your @DatabaseField
. You reference the Object and not the id itself. In the table, the id of the referenced row will be stored.
To clarify for your project:
Office
must look something like this:
@SerializedName("id")
@DatabaseField(id = true, columnName = ID)
private int mId;
...
And your Person
(database-wise) like this:
@DatabaseField(id = true, columnName = ID)
private int mId;
@DatabaseField(columnName = FULL_NAME)
private String mFullName = null;
@DatabaseField(columnName = POSITION)
private String mPosition = null;
@DatabaseField(columnName = EMAIL)
private String mEmail = null;
@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Office office = null;
The Office
column in the Person
table will hold the id
of the Office
.
foreignAutoRefresh
tells ORMLite to fetch the full Office
upon querying the Person
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/field/DatabaseField.html#foreignAutoRefresh()
Hope this helps