我正试着向你询问
posts
打开内部联接的表
users
及其各自的主键和外键:
@Query("SELECT p.*, u.id AS userId, u.name AS userName, u.username AS userUsername FROM posts p JOIN users u ON p.userId = u.id")
fun fetchAllPostsAndUsers(): Observable<List<Post>>
这些是POJO。第一,
User
:
@Entity(tableName = "users")
data class User(
@PrimaryKey(autoGenerate = true)
@SerializedName("id")
@ColumnInfo(name = "id")
val uid: Int,
val name: String,
val username: String,
val email: String,
@Embedded
val address: Address?,
val phone: String,
val website: String,
@Embedded
val company: Company?
)
Post
:
@Entity(
tableName = "posts",
foreignKeys = [ForeignKey(
entity = User::class,
parentColumns = ["id"],
childColumns = ["userId"],
onDelete = ForeignKey.CASCADE
)]
)
data class Post(
@PrimaryKey(autoGenerate = true)
@SerializedName("id")
@ColumnInfo(name = "id")
val pid: Int,
val userId: Int,
val title: String,
val body: String
)
但是上面查询的结果只是
帖子
使用者
,尽管从数据库获取时已包含在查询中。