java - 是否可以在 Android Room 中使用 String 作为 PrimaryKey

标签 java android kotlin android-room

我在我的 java 后端服务器中使用 uuid。所以我需要在房间 android 中使用该 uuid 来确保实体正确同步。 我知道 Is it possible to apply primary key on the text fields in android databasetext as primarykey in android .我想创造这样的东西

@Entity(tableName = CrewColumns.TABLE_NAME)
@TypeConverters(BigDecimalConverter::class)
@JsonIgnoreProperties(ignoreUnknown = true)
class Crew() {
    constructor (uuid: String) : this() {
        this.uuid = uuid;
    }

    /**
     * The unique ID of the item.
     */
    @PrimaryKey
    @ColumnInfo(name = CrewColumns.UUID)
    var uuid: String = ""
    @ColumnInfo(name = CrewColumns.NAME)
    var name: String = ""
}

Room(DAO 等)会有问题吗?谢谢。

最佳答案

是的,您可以使用 String 作为 @PrimaryKey

此外,我还建议使用 Kotlin 的 data class 来简化您的实体。例如:

@Entity
data class Crew(@PrimaryKey val uuid: String, val name: String) {
    // Put any functions or other members not initialized by the constructor here
}

关于java - 是否可以在 Android Room 中使用 String 作为 PrimaryKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47649203/

相关文章:

java - JPA无法更新实体

java - 我可以将 List 作为参数传递给 MyBatis 映射器吗?

android - Google Firestore 保存用户输入 Kotlin

android - 有没有办法重命名桌面上的android程序快捷方式?

android - 如何通过 Glass 上的驱动授权许可页面?

Kotlin 没有看到 Java Lombok 访问器?

kotlin - 字符串模板中无法识别Kotlin扩展属性

java - 设置安卓 :windowIsFloating programmatically

java - Gradle库具有不同的版本

android - 使用 Intent 启动应用程序