With Room 2.2.6 it was possible to define column annotations in an abstract class. Entity classes could extend the abstract class and inherit the column declarations. This allowed to define common columns (e.g. id) in one place and use them in many places.
Abstract class:
abstract class BaseUnit(
@PrimaryKey @ColumnInfo(name = "id") override val id: String,
@ColumnInfo(name = "boxId") override val boxId: String
)
Derived entity class that inherits the declaration of id and boxId:
@Entity(
indices = [Index(BaseUnit.BOX_ID)],
foreignKeys = [
ForeignKey(
entity = Box::class,
parentColumns = arrayOf(Box.ID),
childColumns = arrayOf(BaseUnit.BOX_ID),
onDelete = ForeignKey.CASCADE
)
]
)
data class LevelUnit(
@Ignore override val id: String,
@Ignore override val boxId: String,
@ColumnInfo(name = LEVEL) var level: Int
) : BaseUnit(id, boxId) {
companion object {
const val LEVEL = "level"
}
}
After updating to Room 2.3.0, the code above generates the following build error:
error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
Tried the following constructors but they failed to match:
LevelUnit(java.lang.String,java.lang.String,int) -> [param:id -> matched field:unmatched, param:boxId -> matched field:unmatched, param:level -> matched field:level]
Further observations:
- In the generated JSON schema file the columns
idandboxIdare missing for the LevelUnit table. - There are also consequential errors regarding the foreign
keys and indices, probably because
idandboxIdare missing. - If the
@Ignoreannotation is removed from the inherited fields in theLevelUnitclass, this build error is thrown:error: An entity must have at least 1 field annotated with @PrimaryKey - Room 2.4.0 shows the same behaviour
To me it looks like the annotation processing has changed and that the annotations of the parent class BaseUnit are not evaluated. Has someone experienced the same behaviour or an idea how to solve the issue?