简体   繁体   English

使用 Android 房间关系搜索查询

[英]Search query using Android room relation

In Android room relation, is it possible to use search query using the property of the related table.在 Android 房间关系中,是否可以使用相关表的属性使用搜索查询。 Below is my table structure.下面是我的表结构。 In this i am relating transaction with payment and lines(transaction items).在此,我将交易与付款和行(交易项目)联系起来。 I have an search field in my UI where the user could search using payment amount which is inside payment table.我的 UI 中有一个搜索字段,用户可以使用付款表内的付款金额进行搜索。 How to form a query to access the properties of payment table.如何形成一个查询来访问支付表的属性。

class TransactionWithPaymentAndLines(
    @Embedded
    var transactions: Transactions? = null,

    @Relation(
        parentColumn = "id",
        entityColumn = "transactionId",
        entity = Payment::class
    )
    var payments: List<Payment> = listOf(),

    @Relation(
        parentColumn = "id",
        entityColumn = "transactionId",
        entity = TransactionLines::class
    )
    var transactionLines: List<TransactionLines> = listOf()
)

Absolutely possible, you can use @Query in your DAO class please read Room Database Documentation绝对有可能,您可以在 DAO class 中使用@Query请阅读房间数据库文档

Examples of @Query @Query 的例子

@Query("SELECT * FROM user")
List<User> getAll();

@Query("SELECT * FROM user WHERE uid IN (:userIds)")
List<User> loadAllByIds(int[] userIds);

@Query("SELECT * FROM user WHERE first_name LIKE :first AND " +
       "last_name LIKE :last LIMIT 1")
User findByName(String first, String last);

Ideal way is to query multiple related tables is to create a View .理想的方法是查询多个相关表是创建一个View A view combines data from two or more tables using join .视图使用join组合来自两个或多个表的数据。

In Android, using Room Persistance library , you can create such a view, and then you can query the fields of view.在 Android 中,使用Room Persistance 库,您可以创建这样的视图,然后您可以查询视野。 This is how you can do it:您可以这样做:

Suppose, you have tables:假设你有表:

User : id, name, departmentId用户: id, name, departmentId

Department : id, name部门:身份证,姓名

Create a View:创建视图:

@DatabaseView("SELECT user.id, user.name, user.departmentId," +
        "department.name AS departmentName FROM user " +
        "INNER JOIN department ON user.departmentId = department.id")
data class UserDetail(
    val id: Long,
    val name: String?,
    val departmentId: Long,
    val departmentName: String?
)

Add View to Database:将视图添加到数据库:

@Database(entities = arrayOf(User::class),
          views = arrayOf(UserDetail::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDetailDao(): UserDetailDao
}

Create a DAO:创建一个 DAO:

@Dao
interface UserDetailDao {
    @Query("SELECT * FROM UserDetail")
    fun loadAllUserDetails(): Array<UserDetail>
}

Now, you can query a View using this DAO.现在,您可以使用此 DAO 查询视图。

use @DB or @Query.使用@DB 或@Query。

That should perfectly work...这应该完全有效......

@Query("SELECT * FROM TABLE_NAME") List getAll(); @Query("SELECT * FROM TABLE_NAME") List getAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM