我找不到关于这种关系的任何信息(一切都是关于一对一、一对多或多对多)。甚至那些对于我需要的东西来说看起来也有点太复杂了。
我有一张带有任务的表格和一张带有图像的表格。多个任务可以有相同的图像以节省空间(删除任务时不会删除图像)。我有一个任务的实体
import android.graphics.Bitmap
import androidx.room.*
@Entity(tableName = "tasks")
class Task(
@PrimaryKey(autoGenerate = true)
val id: Long,
val imageId: Long = 0,
// this needs a foreign key to Image
val image: Image
)
另一个用于图像
import androidx.room.ColumnInfo
import androidx.room.PrimaryKey
@Entity(tableName = "images")
class Image (
@PrimaryKey(autoGenerate = true)
val id: Long,
val title: String,
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
val data: ByteArray? = null
)
如何foreignKey为imageId列添加 a 以使其指向 a Image?我可以直接获取一个Image对象作为其成员,Task而无需创建另一个类吗?