给予
@Entity(
tableName = "state",
foreignKeys = arrayOf(
ForeignKey(entity = Foo::class, parentColumns = arrayOf("id"), childColumns = arrayOf("foo_id")),
ForeignKey(entity = Bar::class, parentColumns = arrayOf("id"), childColumns = arrayOf("bar_id"))
)
)
data class State(
@PrimaryKey(autoGenerate = true) val id: Long = 1
) {
@ColumnInfo(name = "foo_id")
var fooId: Long? = null
@ColumnInfo(name = "bar_id")
var barId: Long? = null
}
@Entity(tableName = "foo")
open class Foo(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
open val id: Long,
@ColumnInfo(name = "foo")
val foo: String?,
)
@Entity(tableName = "bar")
open class Bar(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
open val id: Long,
@ColumnInfo(name = "bar")
val bar: String?,
)
我正在尝试创建一个Join POJO来存储查询结果:
class FooBar(
@Embedded
val foo: Foo,
@Embedded
val bar: Bar
)
我的查询失败尝试:
@Query("SELECT foo.*, bar.* FROM foo, bar JOIN state ON foo.id == state.foo_id JOIN bar ON bar.id == session.bar_id ")
fun getFooBar(): LiveData<FooBar>
但是,编译时出现错误。因为它们的名称相同,我是否需要对foo和bar中的id字段进行冲突处理?
我尝试使用前缀,没有运气:
class FooBar(
@Embedded(prefix = "foo_")
val foo: Foo,
@Embedded(prefix = "bar_")
val bar: Bar
)
有什么想法吗?
答案 0 :(得分:0)
这是一个可能对您有所帮助的示例。
@Query("SELECT RoomArticle.*, RoomBranch.id AS 'RoomBranch_id', RoomBranch.name AS 'RoomBranch_name' "
数据实体:
public class RoomArticleOfBranch {
@Embedded
public RoomArticle article;
@Embedded(prefix = "RoomBranch_")
public RoomBranch branch;
请注意,我在查询和@Embedded
中都提供了前缀。
答案 1 :(得分:0)
在尝试嵌入式时,尝试按以下方式更改查询
dateAfter
有两件事需要解决。
在选择查询期间,您需要重命名列名。
修复您的加入查询。