使用 kotlin 时制作实体和 Dao 文件的官方/正确方法是什么

what is the official / right way to make the entity and Dao file when using kotlin

尝试使用kotlin room,找不到官方文档。这是失败的一些发现。

使它适用于简单的情况,但仍然不确定它是否是正确的方法,所以 post 在这里并希望有人知道办公室/使用 kotlin 的正确方法?

有两个问题, 第一个:在定义实体时,它必须放在构造函数中,否则它将编译但不会生成 xx_impl.java,例如:

@Entity(name = "user")
class User {
    @ColumnInfo(name = "id") @PrimaryKey var id: String? = null
    @ColumnInfo(name = "name") var name: String? = null
    @ColumnInfo(name = "lastName") var lastName: String? = null
    @ColumnInfo(name = "age") var age: Int = 0
}

但是如果放在构造函数中

@Entity(tableName = "post")
class DbPost (

@ColumnInfo(name = "title")
var title: String? = null,

@ColumnInfo(name = "authorId")
var authorId: Int? = null,

@ColumnInfo(name = "date")
var date: String? = null,
) {
     @ColumnInfo(name = "id")
     @PrimaryKey(autoGenerate=true)
     var id: Int? = null
}

会生成xxx_impl.java(注意,对于不同的房间版本,有人要求初始化字段,在某些版本中最后一个不能有默认值,在某些版本中所有参数都可以没有初始化值,有谁知道它适用于所有版本的正确方法是什么——也许是最新版本。)

另一个问题是当查询有参数时,编译器似乎会在生成的 xx_impl.java 文件中为其添加自己的名称(让您不知道在生成 xx_impl.java), 在不同的 kotlin 版本中它是不同的。

1。 在一个项目等级中它有 应用插件:'kotlin-kapt'

并且 kotlin_version = '1.1.2-4'

compile "android.arch.persistence.room:runtime:$rootProject.versions.arch_comp"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha1"

@Query("select * from post where id = :id”)
fun findPostById(id: Long): DbPost

error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :arg0.
e: 

e:     public abstract com.manijshrestha.todolist.data.DbPost findPostById(long p0);
                                                       ^

在那里生成的 xxx_impl.java 使用了 p0

@Override
public DbPost findPostById(long p0) {
    final String _sql = "select * from post where id = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    _statement.bindLong(_argIndex, p0);

2。 在其他项目设置中 kotlin_version = '1.1.3-2'

compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha1"

(注意:如果没有kapt "android.arch.persistence.room:compiler:1.0.0-alpha1",则不会生成xxx_impl.java文件)

当在 xxDao 文件中将查询参数放入‘:p0’时

@Query("select * from post where id = :p0")
    fun loadPostById(id: Int): DbPost

它抱怨:

error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :p0.
error: Unused parameter: arg0

在那里生成了 xxx_impl.java 它使用了 arg0

@Override
public DbPost loadPostById(int arg0) {
    final String _sql = "select * from post where id = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    _statement.bindLong(_argIndex, arg0);

@Entity class 中的属性应该添加到构造函数中,因为它也是在 Kotlin 中创建和初始化属性的默认和建议方式。这样您就可以确保您的房间库代码适用于每个版本。 (自从 Kotlin 是官方 android 语言以来,我已经在所有版本中使用并尝试过它)。

对于 DAO class 中的 @Query 方法,参数应该像这样使用:arg0, arg1,..., argN

@Query("select * from post where id = :arg0 and name like :arg1”)
fun findPostByIdName(id: Long, name: String): DbPost

以这种方式使用 @Query 方法将避免 Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :p0. 错误。

Kotlin 刚刚在 8 月 15 日(两天前)发布了其 1.1.4,我不确定此版本中是否允许使用确切的参数名称。

我没有在我的 room-kotlin 实现中使用 apply plugin: 'kotlin-kapt'。它对我不起作用。

有这一行 kapt "android.arch.persistence.room:compiler:1.0.0-alpha1" 是必要的,因为它用于 Room 库的注释处理器,没有它,用于 Room 的注释(如:@Entity@DAO 等)就没有意义实施。

查看这篇文章:https://medium.com/@chandilsachin/room-with-unit-test-in-kotlin-4ad31a39a291

查看这个 Whosebug 问题以获得更多见解:

希望对您有所帮助。