【问题标题】:"Entities and POJOs must have a usable public constructor" with "@androidx.room.Ignore`“实体和 POJO 必须有一个可用的公共构造函数”和“@androidx.room.Ignore`
【发布时间】:2020-12-05 04:42:02
【问题描述】:

我有一个名为 Conversation 的 Room 数据库实体,我想 androidx.room.@Ignore messages 成员变量。但是,当我尝试运行该应用程序时,我收到此错误。

/db/entity/Conversation.java:10: 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 Conversation {
             ^
  Tried the following constructors but they failed to match:
  Conversation(int,java.util.List<com.example.app.db.entity.Message>) -> [param:id -> matched field:id, param:messages -> matched field:unmatched]/db/entity/Conversation.java:12: error: Cannot find setter for field.
    private final int id = 0;

这是实体代码。

/**
 * Entity used to model the conversation SQLite table in the database.
 */
@Entity(tableName = "conversation")
data class Conversation(
    @PrimaryKey val id: Int,
    @Ignore val messages: List<Message>
)

当我注释掉 @Ignore val messages... 行时,应用程序编译。

我已经尝试了 Room 数据库版本 2.2.52.3.0-alpha03 并且都出现了相同的错误。

【问题讨论】:

    标签: android kotlin android-room android-jetpack


    【解决方案1】:

    我能够通过将实体转换为非数据类来解决此问题:

    /**
     * Entity used to model the conversation SQLite table in the database.
     */
    @Entity(tableName = "conversation")
    class Conversation constructor(@PrimaryKey val id: Int) {
    
        @Ignore
        val messages = mutableListOf<Message>()
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 2021-05-01
      • 2020-08-12
      • 2021-12-25
      • 2018-06-24
      • 2017-11-20
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多