当前位置: 首页 > 面试题库 >

Android Room-查询中使用外键

端木阳荣
2023-03-14
问题内容

我有一个Component包含外键来自Component_category

我的目的是通过选择的组件类别来过滤组件列表。

这两个类如下:

成分

@Entity(tableName = "component_table", foreignKeys = {
        @ForeignKey(
            entity = Rack.class,
            parentColumns = "rack_id",
            childColumns = "component_id",
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        ),
        @ForeignKey(
            entity = ComponentCat.class,
            parentColumns = "component_cat_id",
            childColumns = "component_id",
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
})

public class Component {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "component_id")
    public int componentID;

    @NonNull
    @ColumnInfo(name = "component_name")
    private String componentName;

    // .. omitted

组件类别

@Entity(tableName = "component_cat_table")

public class ComponentCat
{
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "component_cat_id")
    public int componentCatID;

    @NonNull
    @ColumnInfo(name = "component_cat_name")
    private String componentCatName;

    // .. omitted

询问

我希望我的查询如下:

 @Query("SELECT * from component_table " +
        "INNER JOIN component_cat_table " +
        "WHERE component_table.component_cat_id == :categoryID ORDER BY component_name ASC")
        LiveData<List<Component>> getFilteredComponents(int categoryID);

但是在编译时它告诉我它无法解决component_table.component_cat_id。我也无法在Component课堂上的外键实体上设置名称。我尝试了一些选项,但在不“访问”外键的情况下不知道如何解决查询。


问题答案:

我碰巧发现答案隐藏在一个名为“ android persistence”的示例项目中。

为了使用外键,还必须定义指定外键的列。列与外键数组之间的“链接”将自动建立。

   @Entity(foreignKeys = {
        @ForeignKey(entity = Book.class,
                parentColumns = "id",
                childColumns = "book_id"),

        @ForeignKey(entity = User.class,
                parentColumns = "id",
                childColumns = "user_id")})
@TypeConverters(DateConverter.class)
public class Loan {
    @PrimaryKey
    @NonNull
    public String id;

    public Date startTime;

    public Date endTime;

    @ColumnInfo(name="book_id")
    public String bookId;

    @ColumnInfo(name="user_id")
    public String userId;
}

上面的示例来自以下链接。

https://github.com/googlecodelabs/android-
persistence/blob/master/app/src/main/java/com/example/android/persistence/codelab/db/Loan.java



 类似资料:
  • 问题内容: 我有以下查询,该查询通常可以正常工作,并且应该返回涵盖定义时间范围的所有行(如果没有绝对匹配,则采用最接近的前一行和后一行-在http://www.orafaq.com/node/1834中概述) 但是希望通过引用外部选择来减少两个表的子选择,但是显然它不喜欢它 有没有一种方法可以使查询不选择三个表? 问题答案: 您可以通过联接执行以下操作: 我不是MySQL专家,因此如果需要一些语法

  • 我刚开始冬眠,所以这个问题可能看起来很愚蠢。 由:org.hibernate.queryException:无法解析属性:Application of:xx.xx.xx.xx.xx.compilation[FROM xx.xx.xx.xx.compilation c,其中c.Application.id=:applicationid]在org.hibernate.queryexception.ge

  • 我做了一个图书馆管理系统。我正试图打印学生问题书,但SQL查询遇到了问题。下面是用户。用于打印发行书籍的php代码。我也上传了数据库,方便大家参考。请帮忙。 使用者php 以下是图书馆管理系统的数据库。 数据库是这样关联的:-usertable将id作为主键。Book将isbn作为主键,bookid作为外键。类型将bookid作为主键。存折的外键为isbn,主键为uid。我想显示存折,其中会显示存

  • 问题内容: 是否可以在使用MySQL的子查询中引用外部查询?我知道在 某些 情况下这是可能的: 但是我想知道这样的事情是否可以工作: 我知道我可以使用或通过将外部子句拉入子查询来实现相同目的,但是我需要这样做来自动生成SQL,并且由于各种其他原因,不能使用任何一种替代方法。 更新 :对不起,这个问题引起了一些混乱:第一个查询只是一个可行的示例,以演示我 不需要的 东西。 更新2 :我需要两个u.i

  • 我刚刚开始使用Spring-Boot的spring data MongoDb。 我主要想要实现的是将上面的本机mongo json查询外部化到配置文件中,并在上面的注释中引用它。 在Hibernate中使用jpa时,Spring数据支持类似的内容。但不确定我们是否可以使用spring data mongodb和spring Boot来做同样的事情。

  • 嗨,我正在尝试使用左外部连接执行hql查询,它是thwhing异常作为org.hibernate.hql.ast.querysyntaxException:意外标记:在第1行附近,可以告诉我这个查询中有什么问题吗 从CreditCardDetails cred左外连接CustomerHistory custHist on cred.creditCardDetailSid=custHist.Cred

  • 样本子句 sample_clause允许您指示数据库从表中的随机数据样本中进行选择,而不是从整个表中进行选择。 我想使用QueryDSL运行下面的查询 样本子句 sample_clause允许您指示数据库从表中的随机数据样本中进行选择,而不是从整个表中进行选择。 从测试t样本(80)中选择,其中t.test_id=01,t.test _ suite _ id = 02 其中条件是动态的,我使用qu

  • 您可以看到,子查询中没有使用协议实体路径a1的别名,而是被协议实体路径A2的别名所取代。在querydsl中还需要做一些其他的事情才能生成这个查询吗?