I looked in the android documentation for an answer to my question, but I couldn't find it. To create a recyclerview using the information contained in these classes, how can I get a list of this information in Room
@Entity(
foreignKeys = [
ForeignKey(
entity = City::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("cityfk"),
onDelete = ForeignKey.NO_ACTION
)
]
)
data class Address(
@PrimaryKey
@ColumnInfo
var id: Long = 0
) : Serializable {
@ColumnInfo
var name: String = ""
@ColumnInfo(index = true)
var cityfk: Long = 0
}
@Entity(
foreignKeys = [
ForeignKey(
entity = State::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("statefk"),
onDelete = ForeignKey.NO_ACTION
)
]
)
data class City(
@PrimaryKey
@ColumnInfo
var id: Long = 0
) : Serializable {
@ColumnInfo
var name: String = ""
@ColumnInfo(index = true)
var statefk: Long = 0
}
@Entity
data class State(
@PrimaryKey
@ColumnInfo
var id: Long = 0
) : Serializable {
@ColumnInfo
var name: String = ""
}
How can I get a list of addresses listing the classes?
How to get a result like this in ANSI SQL:
select ADDRESS.NAME ADDRESS
, CITY.NAME CITY
, STATE.NAME STATE
from ADDRESS
join CITY
on CITY.ID = ADDRES.CITYFK
join STATE
on STATE.ID = CITY.STATEFK
You would typically have a POJO to represent the combined data. You can then either have a field/variable for the extracted columns noting that values are matched to the liked named variable.
You can use @Embedded to include an entity in it's entirety so in theory embed Address City and State.
You can use @Embedded along with @Relation for the child (children) BUT not for grandchildren (e.g. State). You would need an underlying City with State POJO where City is embedded and State is related by an @Relation.
Variable/Column name issues
Room maps columns to variable according to variable names. So there will be issues with id's and name columns if using the simpler @Embedded for all three entities.
I would suggest always using unique names e.g. addressId, cityId, StateId, (at least for the column names e.g. @ColumnInfo(name = "addressId")) but simpler to just have var addressid.
An alternative is the use the @Embedded(prefix = "the_prefix") on some, this tells room to match the variable to column name with the prefix so you need to use AS in the SQL. Obviously the_prefix would be changed to suit.
The Dao's
if using @Embedded with @Relation then you simply need to get the parent so
You would also need the accompanying CityWithState POJO with City @Embedded and State with @Relation.
If Embedding Address, City and State with City having a prefix of "city_" and state having a prefix of "state_" then you would use something like :-
Note the above is in-principle.
Working Example
The following is a working example based upon
First changes to the Address, City and State to rename the columns :-
Address :-
City :-
State :-
Next the POJO AddressWithCityWithState :-
prefix = ?requiredA suitable DAO :-
Using the above :-
The result in the log being :-
Other working examples (without changing column names)
Without any changes to the Entities (Address, city and state). Here are working examples of the other options.
1- Get full address as a single string, all that is required is the query such as :-
2 - Basic POJO with unambiguous column names
The POJO :-
The query :-
3- POJO using EMBEDS
The POJO :-
The query :-
4- POJO's with parent EMBED and child RELATE
The POJO's :-
and :-
and the query :-
Putting the above into use
The following code in an activity uses all 4 to output the same results :-
The output to the log being :-