我在我的 java 后端服务器中使用 uuid。所以我需要在房间 android 中使用该 uuid 来确保实体正确同步。 我知道 Is it possible to apply primary key on the text fields in android database和 text 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/