简体   繁体   English

在房间版本1.1.1中创建复合主键时,无法使用Kotlin编译器构建android studio项目

[英]Unable to build android studio project with Kotlin compiler while creating composite primary key in room version 1.1.1

While I am creating an 'Entity' with a 'primaryKey' annotation over a field to create any SQLite table using room, the Android Studio project builds successfully. 当我在一个字段上创建一个带有'primaryKey'注释的'Entity'来创建任何使用room的SQLite表时,Android Studio项目会成功构建。 But while I am trying to create an 'Entity' with composite primary key, unable to build the project. 但是,当我尝试使用复合主键创建“实体”时,无法构建项目。 Unfortunately as I am using 'Dagger 2' in my project unable to see the actual build error. 不幸的是,因为我在我的项目中使用'Dagger 2'无法看到实际的构建错误。

Note : Project complies with Kotlin compiler. 注意 :Project符合Kotlin编译器。

Code : 代码

@Entity(tableName = "thread_users", primaryKeys = {"thread_id", "user_id"})
public class DMThreadUsers {
    @ColumnInfo(name = "thread_id")
    private String threadId;
    @ColumnInfo(name = "user_id")
    private String userId;
    @ColumnInfo(name = "is_left")
    private boolean isLeft;

    public DMThreadUsers() {
    }

    public String getThreadId() {
        return threadId;
    }

    public void setThreadId(String threadId) {
        this.threadId = threadId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public boolean isLeft() {
        return isLeft;
    }

    public void setLeft(boolean left) {
        isLeft = left;
    }
}

Primary key fields should be annotated with @NonNull. 主键字段应使用@NonNull注释。 You have to annotate @NonNull in JAVA for primary key fields and in KOTLIN one need to initialise the primary key fields. 您必须在JAVA中为主键字段注释@NonNull,在KOTLIN中需要初始化主键字段。

@Entity(tableName = "thread_users", primaryKeys = {"thread_id", "user_id"})
public class DMThreadUsers {
    @NonNull
    @ColumnInfo(name = "thread_id")
    private String threadId;
    @NonNull
    @ColumnInfo(name = "user_id")
    private String userId;
    @ColumnInfo(name = "is_left")
    private boolean isLeft;

    public DMThreadUsers() {
    }

    public String getThreadId() {
        return threadId;
    }

    public void setThreadId(String threadId) {
        this.threadId = threadId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public boolean isLeft() {
        return isLeft;
    }

    public void setLeft(boolean left) {
        isLeft = left;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM