Sam*_*hen 3 android android-database android-room android-jetpack
我的实体类:
@Entity(tableName = "student")
data class Student(
var name: String,
var age: Int,
var gpa: Double,
var isSingle: Boolean,
@PrimaryKey(autoGenerate = true)
var id: Long = 0,
@Ignore //don't create column in database, just for run time use
var isSelected: Boolean = false
)
Run Code Online (Sandbox Code Playgroud)
然后我像这样插入(在 中测试androidTest):
val student = Student("Sam", 27, 3.5, true)
studentDao.insert(student)
Run Code Online (Sandbox Code Playgroud)
添加@Ignore注释后,它立即给了我这个错误:
C:\Android Project\RoomTest\app\build\tmp\kapt3\stubs\debug\com\example\roomtest\database\Student.java:7: ????: 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 Student {
^
Tried the following constructors but they failed to match:
Student(java.lang.String,int,double,boolean,boolean,long) -> [param:name ->
matched field:name, param:age -> matched field:age, param:gpa -> matched
field:gpa, param:isSingle -> matched field:isSingle, param:isSelected ->
matched field:unmatched, param:id -> matched field:id][WARN] Incremental
annotation processing requested, but support is disabled because the
following processors are not incremental: androidx.room.RoomProcessor
(DYNAMIC).
Run Code Online (Sandbox Code Playgroud)
由于 Room 在编译期间仍然会生成 Java 类并且问题在于默认值参数,因此尝试使用@JvmOverloads作为构造函数:
@Entity(tableName = "student")
data class Student @JvmOverloads constructor(
var name: String,
.....
Run Code Online (Sandbox Code Playgroud)
更新
有趣的是,在文档中没有提到对这种情况的特殊处理。并且此问题已存在以修复此文档。
从这个问题至于问题本身的结论是:
使用@JvmOverloads 应该可以工作。在不久的将来有一天,我们可能会生成 Kotlin,这不会成为问题
| 归档时间: |
|
| 查看次数: |
464 次 |
| 最近记录: |