1
votes

I've tried to convert immutable Java class to Kotlin, but failed.

Preconditions:

global build.gradle:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

module build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
kapt "android.arch.persistence.room:compiler:$roomVersion"
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

Java Room entity:

@Immutable
@Entity
public class User {
    @PrimaryKey
    @SerializedName("user_id")
    @Expose
    private final int userId;
    @SerializedName("display_name")
    @Expose
    private final String userName;
    @SerializedName("amount")
    @Expose
    private final String amount;
    @SerializedName("has_access")
    @Expose
    private final String hasAccess;

    public User(final int userId, final String userName,
                final String amount, final String hasAccess) {
        this.userId = userId;
        this.userName = userName;
        this.amount = amount;
        this.hasAccess = hasAccess;
    }

    public int getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }

    public String getAmount() {
        return amount;
    }

    public String getHasAccess() {
        return hasAccess;
    }
} 

Same entity converted to Kotlin:

class User(@field:PrimaryKey
                @field:SerializedName("user_id")
                @field:Expose
                val userId: Int,
                @field:SerializedName("display_name")
                @field:Expose
                val userName: String,
                @field:SerializedName("amount")
                @field:Expose
                val amount: String,
                @field:SerializedName("has_access")
                @field:Expose
                val hasAccess: String)

Java entity works without any problems, but convertion to Kotlin leads to the next buid errors:

e: 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).
e: error: Cannot find setter for field.

Summary question: how to properly use immutable Kotlin entities with Room persistence library?

UPDATE: User database and dao are located in the seperate from User model module. It seems that Room doesn't work with @NotNull, @NonNull, @Nullable, whathever in the constructor parentheses which are added by Kotlin for val properties. Even adding them to the java constructor leads to same errors.

1

1 Answers

1
votes

Instead of using plain java class use data class in kotlin, you'll get all the getter, equals, hashCode methods right of the bat.

data class User(@PrimaryKey @ColumnInfo(name = "user_id") val userId: Long,
            @ColumnInfo(name ="display_name") val userName: String = "",
            @ColumnInfo(name ="amount") val amount: String = "",
            @ColumnInfo(name ="has_access") val hasAccess: String = "")