是否可以在 Android 房间中使用字符串作为主键
Is it possible using String as PrimaryKey in Android Room
我在 java 后端服务器中使用 uuid。所以我需要在房间 android 中使用那个 uuid 来确保实体正确同步。
我知道 Is it possible to apply primary key on the text fields in android database and 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 后端服务器中使用 uuid。所以我需要在房间 android 中使用那个 uuid 来确保实体正确同步。 我知道 Is it possible to apply primary key on the text fields in android database and 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
}