I am currently trying to get results from a query of nested relationships in Room. Here Are my classes/database entities involved:
@Entity
data class PrayerRequestEntity(
var title: String,
var details: String,
var category: String,
var isAnswered: Boolean,
var prayerCount: Int,
var dateCreated: Date,
var dateUpdated: Date
) {
@PrimaryKey(autoGenerate = true)
var prayerRequestId: Int = 0
}
@Entity
data class PrayerPlanEntity(
var title: String,
var sessionCount: Int,
var dateCreated: Date,
var dateUpdated: Date
) {
@PrimaryKey(autoGenerate = true)
var planId: Int = 0
}
@Entity(primaryKeys = ["planId", "prayerRequestId"])
data class PrayerPlanPrayerRequestJoinEntity(
val planId: Int,
val prayerRequestId: Int
)
I am trying to get a list of PrayerPlanEtities along with the PrayerRequestEntities associated with each and have no problem using the following return:
data class PrayerPlanPrayerRequestsJoin(
@Embedded
val prayerPlan: PrayerPlanEntity,
@Relation(
parentColumn = "planId",
entityColumn = "prayerRequestId",
associateBy = Junction(PrayerPlanPrayerRequestJoinEntity::class)
)
val prayers: List<PrayerPlanEntity>
)
I need to also query the relationships of PrayerRequestEntity in that same query so Imodify the aforementioned class like so:
data class PrayerPlanPrayerRequestsJoin(
@Embedded
val prayerPlan: PrayerPlanEntity,
@Relation(
parentColumn = "planId",
entityColumn = "prayerRequestId",
associateBy = Junction(PrayerPlanPrayerRequestJoinEntity::class)
)
val prayers: List<PrayerRequestJoin>
)
PrayerRequestJoin is a previous relationship I created which works perfectly fine on it own and looks like so:
data class PrayerRequestJoin(
@Embedded
val prayerRequest: PrayerRequestEntity,
@Relation(
parentColumn = "prayerRequestId",
entityColumn = "prayerRequestCategoryId",
associateBy = Junction(PrayerRequestCategoryJoinEntity::class)
)
val category: PrayerRequestCategoryEntity,
@Relation(
parentColumn = "prayerRequestId",
entityColumn = "photoId",
associateBy = Junction(PrayerRequestPhotoJoinEntity::class)
)
val photos: List<PhotoEntity>,
@Relation(
parentColumn = "prayerRequestId",
entityColumn = "noteId",
associateBy = Junction(PrayerRequestNoteJoinEntity::class)
)
val notes: List<NoteEntity>
)
But now I am getting two build errors an Android Studio:
error: constructor PrayerPlanPrayerRequestsJoin in class PrayerPlanPrayerRequestsJoin cannot be applied to given types; _item = new PrayerPlanPrayerRequestsJoin(); ^ required: PrayerPlanEntity,List found: no arguments reason: actual and formal argument lists differ in length
and also:
error: prayerPlan has private access in PrayerPlanPrayerRequestsJoin _item.prayerPlan = _tmpPrayerPlan;
Can anyone provide some insight on what may be causing this issue?
I believe that your issue is with the @Query's rather than with the relationships.
Upon closer inspection the messages shows java code e.g. termination with
;'s.Therefore the issue is within the generated java and it would appear (from creating a working example) that the issue is in the code underlying the class or classes annotated with @Dao
i.e.
_itemdoes not appear in the java for the class annotated with@Database. Whilst _item = new .... appears for each @Query annotated function. e.g.for a Query using
SELECT * FROM PrayerPlanPrayerRequestJoinEntitythen the code includes_item = new PrayerPlanPrayerRequestJoinEntity(_tmpPlanId,_tmpPrayerRequestId);for a Query using
"SELECT * FROM prayerRequestEntity"then the code includes_item = new PrayerRequestJoin(_tmpPrayerRequest,_tmpCategory_1,_tmpPhotosCollection_1,_tmpNotesCollection_1);As can be seen these are used to build the final result. The first example being the closest from the working example but there is no issue.
As such I believe that the issue is not with the relationships (working example runs OK) but the issue is with the @Query annotated functions.
I would suggest commenting them all out and adding each until you find the culprit.
The working example
Note that this example introduces/uses some shortcuts so the code differs a little.
The code:-
NoteEntity made up just to test, so very simple
PhotoEntity made up ....
PrayerPlanEntity
PrayerPlanPrayerRequestJoinEntity
PrayerPlanPrayerRequestsJoin assume the 2nd is as it is now
PrayerRequestCategoryEntity made up ....
PrayerRequestCategoryJoinEntity made up ....
PrayerRequestEntity
PrayerRequestJoin
PrayerRequestNoteJoinEntity made up
PrayerRequestPhotoJoinEntity made up ....
AllDAO made up
TheDatabase made up
Last but not least an activity to do a simple test of the related data via the getAllPrayerRequestsWithCategoryNotesAndPhotos query:-
Finally the result from the Log:-