Andreas1234
Andreas1234

Reputation: 698

Why I get the class must be either @Entity or @DatabaseView error from room?

So, I would like to implement one to one relation between CrmLeadActivity and CrmLeadActivityRecurrence tables. And after that, I want to use this tablet inside an another table where the relation is N-M.

If I replace CrmLeadActivityWithRecurrence to CrmLeadActivity inside CrmOpportunityWithEventsView table, the error is gone. But I loose CrmLeadActivityRecurrence table.

Inside the CrmOpportunityRepository I created a Raw Query.

The specific errors are :

CrmLeadActivityWithRecurrence.java:8: error: The class must be either @Entity or @DatabaseView.

Cannot find the child entity column `crm_opportunity_oid` in hu.crm.crmapp.ui.uimodels.CrmLeadActivityWithRecurrence

I dont understand the second error, because as you can see, CrmLeadActivity contains the crm_opportunity_oid

All the entity models are in my DateBaseMondule and the Dao classes as well

So My models:

CrmLeadActivity

@Entity(tableName = CrmLeadActivity.TABLE_NAME,indices = [Index(value = ["subject","start_on","crm_opportunity_oid"])])
@JsonIgnoreProperties(ignoreUnknown = true)
open class CrmLeadActivity(
    @JsonProperty("oid") @PrimaryKey @ColumnInfo(name = "oid") var oid: String = "",
    @JsonProperty("Subject") @ColumnInfo(name = "subject") var subject: String? = null,
    @JsonProperty("StartOn") @ColumnInfo(name = "start_on") var startOn: Date = Date(),
    @JsonProperty("EndOn") @ColumnInfo(name = "end_on") var endOn: Date = Date(),
    @JsonProperty("CrmOpportunityOid") @ColumnInfo(name = "crm_opportunity_oid") var crmOpportunityOid: String = "",
    @JsonProperty("Location") @ColumnInfo(name = "location") var location: String? = null,
    @JsonProperty("Description") @ColumnInfo(name = "description") var description: String? = null,
    @JsonProperty("SendNotificationToParticipant") @ColumnInfo(name = "send_notification_to_participants") var sendNotificationToParticipants: Boolean = false,
    @JsonProperty("SyncStatus") @ColumnInfo(name = "sync_status") var syncStatus: Int? = null,
    @JsonProperty("CrmLeadActivityHrPersonRows") @Ignore open var crmLeadActivityHrPersonRows : List<CrmLeadActivityHrPerson> = emptyList()
)

CrmLeadActivityRecurrence

@Entity(tableName = CrmLeadActivityRecurrence.TABLE_NAME)
@JsonIgnoreProperties(ignoreUnknown = true)
open class CrmLeadActivityRecurrence(
    @PrimaryKey @ColumnInfo(name = "oid") var oid: String = "",
    @ColumnInfo(name = "start_time") var startTime: Date? = null,
    @ColumnInfo(name = "end_time") var endTime: Date? = null,
    @ColumnInfo(name = "date_number") var dateNumber: Int? = null,
    @ColumnInfo(name = "recurrence_type") var recurrenceType: Int? = null,
    @ColumnInfo(name = "crm_lead_activity_oid") var crmLeadActivityOid: String = "",
    @ColumnInfo(name = "day_of_week") var dayOfWeek: Int? = null,
    @ColumnInfo(name = "recurrence_range") var recurrence_range: Int? = null
)

Relation Table:

data class CrmLeadActivityWithRecurrence(
    @Relation(
        parentColumn = "oid",
        entityColumn = "crm_lead_activity_oid",
    )
    var crmLeadActivityRecurrence: CrmLeadActivityRecurrence? = null
): CrmLeadActivity()

Another Table:

@Entity(tableName = CrmOpportunity.TABLE_NAME,indices = [Index(value = ["short_name","customer_oid","sale_date","close_date","guidance_date","mapping_date"])])
@JsonIgnoreProperties(ignoreUnknown = true)
open class CrmOpportunity(
    @JsonProperty("Oid") @PrimaryKey @ColumnInfo(name = "oid") var oid: String = "",
    @JsonProperty("ShortName") @ColumnInfo(name = "short_name") var shortName: String? = "",
    @JsonProperty( "CustomerOid") @ColumnInfo(name = "customer_oid") var customerOid: String = ""

//There are more propoerties, but I think it is not important
}

And another Relation table:

class CrmOpportunityWithEventsView(
    @Relation(
        parentColumn = "oid",
        entityColumn = "crm_opportunity_oid"
    )
    var crmLeadActivities: List<CrmLeadActivityWithRecurrence> = emptyList()
) : CrmOpportunity() {}

Repository:

fun getProjectWithEvent(
        searchText: String?,
        fromDate: Long,
        toDate: Long,
        hrPerson: String?
    ): LiveData<PagedList<CrmOpportunityWithEventsView>> {
        val sb = StringBuilder()
        val bindLists = ArrayList<Any>()

        sb.append("SELECT t1.* , count(t2.oid) FROM ${CrmOpportunity.TABLE_NAME} t1 LEFT JOIN ${CrmLeadActivity.TABLE_NAME} t2 ON t1.oid = t2.crm_opportunity_oid ")
        sb.append(" LEFT JOIN ${CrmLeadActivityHrPerson.TABLE_NAME} t3 ON t2.oid = t3.crm_lead_activity_oid ")
        sb.append(" LEFT JOIN ${CrmLeadActivityRecurrence.TABLE_NAME} t4 ON t2.oid = t4.crm_lead_activity_oid ")
        sb.append("WHERE (t1.short_name like ? ")
        bindLists.add(CommonUtils.sqlLikeQuery(searchText,true))
        sb.append(" OR t2.subject like ? ) ")
        bindLists.add(CommonUtils.sqlLikeQuery(searchText,true))

        sb.append(" AND t3.hr_person_oid LIKE ?")
        bindLists.add(CommonUtils.sqlLikeQuery(hrPerson,true))

        sb.append(" AND ((t2.start_on >= ? ")
        bindLists.add(fromDate)
        sb.append(" AND t2.start_on <= ? ) OR")
        bindLists.add(toDate)

        sb.append(" ( t4.start_time <= ? AND t4.end_time >= ? ))")
        bindLists.add(fromDate)
        bindLists.add(fromDate)

        sb.append(" GROUP BY t1.oid HAVING count(t2.oid) > 0 ORDER BY t1.oid")

        val simpleSqLiteQuery = SimpleSQLiteQuery(sb.toString(), bindLists.toTypedArray())

        val config = PagedList.Config.Builder()
            .setEnablePlaceholders(true)
            .setPageSize(50)
            .setPrefetchDistance(50)
            .setInitialLoadSizeHint(50 * 2).build()

        return LivePagedListBuilder(
            crmOpportunityDao.getProjectWithEvents(simpleSqLiteQuery),
            config
        ).build()
    }

Dao:

@RawQuery(observedEntities = [CrmOpportunity::class, CrmLeadActivity::class,CrmLeadActivityHrPerson::class,CrmLeadActivityRecurrence::class])
abstract fun getProjectWithEvents(simpleSQLiteQuery: SimpleSQLiteQuery): DataSource.Factory<Int, CrmOpportunityWithEventsView>

Upvotes: 1

Views: 870

Answers (1)

MikeT
MikeT

Reputation: 57053

Issue

I dont understand the second error, because as you can see, CrmLeadActivity contains the crm_opportunity_oid

BUT the message is not saying that CrmLeadActivity should or does not contain the crm_opportunity_oid.

The message is saying that crm_opportunity_oid cannot be found in CrmLeadActivityWithRecurrence.

This would be because of :-

class CrmOpportunityWithEventsView(
    @Relation(
        parentColumn = "oid",
        entityColumn = "crm_opportunity_oid"
    )
    var crmLeadActivities: List<CrmLeadActivityWithRecurrence> = emptyList()
) : CrmOpportunity() {}

So message CrmLeadActivityWithRecurrence.java:8: error: The class must be either @Entity or @DatabaseView. is saying that CrmLeadActivityWithRecurrence is not a table, which it isn't, it's a POJO that is instantiated with columns/fields from the related tables (CrmLeadActivity.TABLE_NAME and CrmLeadActivityRecurrence.TABLE_NAME).

Fix

As such you need to identify the table using the @Relation's entity parameter to let room know the table where it is expected to get the crm_opportunity_oid from.

  • By default (omission of the entity = ?::class) @Relation defaults to the table associated with the var/val that the annotation is for. So it tries to look in the table associate with CrmLeadActivityWithRecurrence and it's not a table (first message) then it says (2nd Message) it cannot find the column crm_oppurtunity_oid as CrmLeadActivityWithRecurrence isn't a table.

As such I believe that you want :-

class CrmOpportunityWithEventsView(
    @Relation(
        entity = CrmLeadActivity::class, /*<<<<<<<<<< ADDED */
        parentColumn = "oid",
        entityColumn = "crm_opportunity_oid"
    )
    var crmLeadActivities: List<CrmLeadActivityWithRecurrence> = emptyList()
) : CrmOpportunity() {}
  • Note the code for the fix has not been compiled or run and may therefore contain errors/typos, it is intended solely as in-principle code.

Upvotes: 3

Related Questions