我正在使用Room Persistance Library(v2.0.0)查询POJO。我将所有这些实体都放在不同的表中(簿记,类别等)。
我正在尝试将BookEntryEntity与与BookEntry的category_id
相匹配的类别相结合。
class BookEntry(
@Embedded var entity: BookEntryEntity = BookEntryEntity(),
@Embedded(prefix = 'cg_') var category: Category? = null,
contacts: List<Contact.ContactEntity>? = null
) : Parcelable {
@Relation(
entity = Contact.ContactEntity::class,
parentColumn = 'id',
entityColumn = 'bookentry_id'
)
var embeddedContacts: List<Contact.ContactEntity>? = contacts
//...
@Entity(tableName = 'bookentries')
data class BookEntryEntity(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = 'id') var id: Long = 0,
@ColumnInfo(name = 'title') var title: String = "",
@ColumnInfo(name = 'date') var date: Date = Date(),
@ColumnInfo(name = 'value') var value: Double = 0.0,
@ColumnInfo(name = 'notes') var notes: String = "",
@ColumnInfo(name = 'entrytype') var entryType: Int = Type.Earning,
@ColumnInfo(name = Reference.CATEGORY_ID) var categoryId: Long? = null
)
}
嵌入实体Category
不会自动加入。
@Entity(tableName = 'categories')
data class Category(@PrimaryKey(autoGenerate = true) @ColumnInfo(name = 'id') val id: Long = 0, @ColumnInfo(name = 'name') var name: String = "")
我尝试通过以下方式加入它:
@Dao
interface BookEntryDao {
@Transaction
@Query("SELECT * FROM bookentries INNER JOIN categories ON bookentries.cg_id = categories.id WHERE categories.id == bookentries.category_id ORDER BY bookentries.date DESC")
fun getBookEntries(): LiveData<List<BookEntry>>
}
不幸的是,我遇到一个错误:
错误:查询存在问题:[SQLITE_ERROR] SQL错误或 缺少数据库(无此列:bookentries.cg_id)
但是,如果我删除内部联接,则会收到以下警告:
警告:at.guger.moneybook.data.model.BookEntry有一些字段 查询未返回的[cg_id,cg_name]。
如何正确加入查询?
答案 0 :(得分:1)
尝试:
@Dao
interface BookEntryDao {
@Transaction
@Query("SELECT * FROM bookentries INNER JOIN categories ON bookentries.category_id = categories.id ORDER BY bookentries.date DESC")
fun getBookEntries(): LiveData<List<BookEntry>>
}