找不到字段的设置器 - 使用 Kotlin 和 Room 数据库 [英] Cannot find setter for field - using Kotlin with Room database
问题描述
我正在与 Room 持久性库集成.我在 Kotlin 中有一个数据类,例如:
@Entity(tableName = "story")数据类 Story (@PrimaryKey 有效 ID:长,val by:字符串,val 后代:Int,val 分数:Int,有效时间:长,val 标题:字符串,val 类型:字符串,val url: 字符串)
@Entity
和 @PrimaryKey
注释用于 Room 库.当我尝试构建时,它失败并显示错误:
错误:找不到字段的设置器.错误:任务 ':app:compileDebugJavaWithJavac' 的执行失败.>编译失败;有关详细信息,请参阅编译器错误输出.
我也尝试提供默认构造函数:
@Entity(tableName = "story")数据类 Story (@PrimaryKey 有效 ID:长,val by:字符串,val 后代:Int,val 分数:Int,有效时间:长,val 标题:字符串,val 类型:字符串,val url: 字符串){构造函数() : this(0, "", 0, 0, 0, "", "", "")}
但这也行不通.需要注意的是,如果我将这个 Kotlin 类转换为带有 getter 和 setter 的 Java 类,它会起作用.任何帮助表示赞赏!
由于您的字段用 val
标记,因此它们实际上是最终的并且没有 setter 字段.
尝试将 val
换成 var
.您可能还需要初始化字段.
@Entity(tableName = "story")数据类 Story (@PrimaryKey var id:长?= 空,var by: String = "",var后代:Int = 0,var 分数:Int = 0,无功时间:长 = 0L,var title: String = "",var 类型:String = "",var url: 字符串 = "")
编辑
当 Kotlin 与其他 Java 库(如 Hibernate)一起使用时,上述解决方案是对 Kotlin 中此错误的一般修复,我也看到了这一点.如果您想保持 Room 的不变性,请参阅其他一些可能更适合您的情况的答案.
在某些情况下,Java 库的不变性根本不起作用,虽然让开发人员感到悲伤,但不幸的是,您必须将 val
切换为 var
.>
I'm integrating with the Room persistence library. I have a data class in Kotlin like:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
)
The @Entity
and @PrimaryKey
annotations are for the Room library. When I try to build, it is failing with error:
Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
I also tried providing a default constructor:
@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) {
constructor() : this(0, "", 0, 0, 0, "", "", "")
}
But this doesn't work as well. A thing to note is that it works if I convert this Kotlin class into a Java class with getters and setters. Any help is appreciated!
Since your fields are marked with val
, they are effectively final and don't have setter fields.
Try switching out the val
with var
.
You might also need to initialize the fields.
@Entity(tableName = "story")
data class Story (
@PrimaryKey var id: Long? = null,
var by: String = "",
var descendants: Int = 0,
var score: Int = 0,
var time: Long = 0L,
var title: String = "",
var type: String = "",
var url: String = ""
)
EDIT
The above solution is a general fix for this error in Kotlin when using Kotlin with other Java libraries like Hibernate where i've seen this as well. If you want to keep immutability with Room, see some of the other answers which may be more specific to your case.
In some cases immutability with Java libraries is simply not working at all and while making sad developer noises, you have to switch that val
for a var
unfortunately.
这篇关于找不到字段的设置器 - 使用 Kotlin 和 Room 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!