Architecture-samples: 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), related to @Ignore

Created on 21 Mar 2019  路  2Comments  路  Source: android/architecture-samples

I am encountering the issue like stated in the title. I have the following entities:

@Entity
data class FichePrItem(
    @PrimaryKey var id: String,
    var observation: String?,
    @Ignore var document: Documents,
    @Embedded var action: Action,
    var isSelected: Boolean = false
) {
    var documentId = document.id
}

class Action(@SerializedName("libelleDisplay") val decision: String?)

@Entity
data class Documents(
    @PrimaryKey var id: String,
    var objet: String?,
    @SerializedName("referenceArchive") var numeroEnrolement: String?,
    @Ignore var ministere: Ministeres
) {
    var ministereId: String = ministere.id
}

@Entity
data class Ministeres(
    @PrimaryKey var id: String,
    @SerializedName("code")
    var sigle: String?
)

When building my project, i get the following gradle errors:

e: /Users/armelus/StudioProjects/opensi/FichePRSample/econseildb/build/tmp/kapt3/stubs/debug/co/opensi/econseildb/FichePrItem.java:7: 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).
public final class FichePrItem {
             ^
  Tried the following constructors but they failed to match:
  FichePrItem(java.lang.String,java.lang.String,co.opensi.econseildb.Documents,co.opensi.econseildb.Action,boolean) -> [param:id -> matched field:id, param:observation -> matched field:observation, param:document -> matched field:unmatched, param:action -> matched field:action, param:isSelected -> matched field:isSelected]
e: /Users/armelus/StudioProjects/opensi/FichePRSample/econseildb/build/tmp/kapt3/stubs/debug/co/opensi/econseildb/Documents.java:7: 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).
public final class Documents {
             ^
  Tried the following constructors but they failed to match:
  Documents(java.lang.String,java.lang.String,java.lang.String,co.opensi.econseildb.Ministeres) -> [param:id -> matched field:id, param:objet -> matched field:objet, param:numeroEnrolement -> matched field:numeroEnrolement, param:ministere -> matched field:unmatched]

When i comment @Ignore lines, the build finish successfully otherwise, it fails

Can somebody help me please?

implementation "androidx.room:room-runtime:2.1.0-alpha05"
kapt "androidx.room:room-compiler:2.1.0-alpha05"

Most helpful comment

Hi!
You have to move your @Ignore fields out of constructor and set default value for them. For example:

@Entity
data class FichePrItem(
    @PrimaryKey var id: String,
    var observation: String?,
    @Embedded var action: Action,
    var isSelected: Boolean = false
) {
    @Ignore var document: Documents = Documents()
// or @Ignore var document: Documents? = null
    var documentId = document.id
}

All 2 comments

Hi!
You have to move your @Ignore fields out of constructor and set default value for them. For example:

@Entity
data class FichePrItem(
    @PrimaryKey var id: String,
    var observation: String?,
    @Embedded var action: Action,
    var isSelected: Boolean = false
) {
    @Ignore var document: Documents = Documents()
// or @Ignore var document: Documents? = null
    var documentId = document.id
}

Hi!
You have to move your @Ignore fields out of constructor and set default value for them. For example:

@Entity
data class FichePrItem(
    @PrimaryKey var id: String,
    var observation: String?,
    @Embedded var action: Action,
    var isSelected: Boolean = false
) {
    @Ignore var document: Documents = Documents()
// or @Ignore var document: Documents? = null
    var documentId = document.id
}

Thanks, @borysmatveichuk,
Thank you for your answer, I tried it and it works well. But doing so does not really suit me because I would not like to be forced to assign a value by default.
Is there really no other way to settle this?
In fact when I use version 1.1.1 (without androidX), I see that I do not have this kind of error, is it an internal bug to the librairy under androidX?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohitajwani picture mohitajwani  路  4Comments

AeroEchelon picture AeroEchelon  路  4Comments

iraj-wisilica picture iraj-wisilica  路  6Comments

BeQuietLee picture BeQuietLee  路  3Comments

aballano picture aballano  路  5Comments