问题标签 [android-room-relation]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
36 浏览

android - Android房间数据库删除不起作用

我正在使用 Room DB 来获取回收站视图。每行都有一个删除图标来删除该项目。我也想从 Room db 中删除相同的项目。我已经完成了下面的代码,但它只反映在列表中,刷新后再次从 db 加载已删除的数据。

带有删除侦听器的适配器类:

数据库道

请帮助我。

0 投票
2 回答
65 浏览

kotlin - Best way to deserialize Json with nested objects into Android Room Entity with ForeignKeys

I have a Client api. The json response looks like this:

So, using the Kotlin data class file from JSON to automatically create data classes from the json response, I've got with the following Client data class which I've turned into a Room @Entity with ForeignKeys:

There's also PaymentType and Person data classes which I'm omitting, but they do are Room @Entity's as well.

The Room database needs to match the following database structure that has this CREATE TABLE SQL statement:

So, I've a Converters class to deserialize the json response into Client class as follows:

I'm hesitant if the client converter above does correctly creates PaymentType and Person objects automatically (mostly convinced that no). That's why I would like to know what's the proper way to deserialize a json response with nested objects into Room entities with Foreign Keys?

I'm most confused with Foreing Keys though. What will happen when the converter above tries to parse the "person": {} object into the @ColumnInfo(name = "person_id") which is of type Int? Will it know that it is a ForeignKey and will create a Person::class automatically? How's the best/proper way to deserialize nested objects ensuring this relation between the tables is properly done?

0 投票
2 回答
86 浏览

java - 对于 Room 中的嵌套对象,我们应该怎么做?

如果有像我下面的 JSON 结构这样的结构,我们应该如何创建实体类?没有这方面的例子。虽然在很久以前写的文章中,@embeded 用于内部数组,但现在使用了像转换器这样的结构。我们应该使用哪一个?这些有什么作用?如何创建我的类型的结构?请帮助Java

所有需要的结构都可以在这里找到:https ://github.com/theoyuncu8/roomdb

JSON数据

实体类

食品类

FoodUnitsData 类

0 投票
0 回答
22 浏览

android-room - 带分页的房间关系查询方法

Room 2.4中,DAO 中有一个称为关系查询方法的新功能,您可以编写自定义查询以从 2 个实体中选择列,并且 Room 可以聚合成Map<TableA, List<TableB>>返回类型。

我有一个相当复杂的查询,它确实与嵌套查询左连接以返回火车站地图及其相关铁路线(多对多关系)。我试图让一个@Query方法返回Flow<Map<RailStation, List<RailLine>>>,它可以返回我想要的地图。

现在,我想更进一步让它返回 page 3's PagingSource。由于原来的类型是 a Map,所以我想我应该把分页@Query方法设为PagingSource<Int, Map.Entry<RailStation, List<RailLine>>>。(Map.Entry应该是单个列表项的代表类型,而不是Map代表整个查询结果。)但是,Room 注释处理器抱怨这一行说它无法处理这种类型:

所以我的问题是:Room 注释处理器和 Paging 3 是否支持使用 paging 3 的关系查询方法?如果没有,是否有其他方法可以归档相同的目标?似乎@RelationRoom 中的注解只能支持简单的表连接,但我的情况是我需要在LEFT JOIN子句中编写嵌套查询。

0 投票
1 回答
23 浏览

android - Android room DB - 处理数据库升级和更改表中的外键

我正在使用房间数据库。我有 2 张桌子 A 和 B。

A的主键是表B中的外键。

以下是我的数据库结构 -

现在我想从房间数据库中删除表 A。但是表 B 将在那里。那么我应该如何处理这种迁移呢?我特别注意删除 foreignKeys 约束。无法弄清楚。我认为应该在删除父表 A 之前将其删除。

表 B 中的某些列也将被删除。

所以这就是我正在考虑的方法 -

  1. 删除外键约束(需要解决方案如何做到这一点?)
  2. 删除表 A
  3. 更改表 B

请在第 1 点指导我,并建议我是否有更好的方法来处理这个问题。

谢谢。