简体   繁体   English

android- Room数据库中同一类的多个表彼此之间没有关系

[英]android- Multiple tables of the same class in Room Database with no relation to each other

I'm using Room as the database for the app. 我正在使用Room作为该应用程序的数据库。 I have a scenario where an Object of a certain type needs to be stored in separate tables. 我有一种情况,其中某种类型的对象需要存储在单独的表中。 As an example, lets take the Object called Item.java 作为示例,让我们以名为Item.java的对象为例

Now, I want to have three SQL tables: 现在,我想拥有三个SQL表:

Item1 项目1

Item2 项目2

Item3 项目3

ignore any naming conventions for SQL DB please - this is just an example 请忽略SQL DB的任何命名约定-这只是一个示例

Problem: 问题:

I have an entity as @Entity(tableName="Item1") in the Item.java class and have a DAO class that will use the table name. 我在Item.java类中具有一个@Entity(tableName =“ Item1”)实体,并具有将使用表名的DAO类。

I am having 3 fragments, each being independent of each other. 我有3个片段,每个片段彼此独立。 I just want three database with same attributes and functions as Item. 我只想要三个与Item具有相同属性和功能的数据库。 I want to create another table Item2 of same Item.java class to use it in the second fragment. 我想创建另一个具有相同Item.java类的表Item2,以在第二个片段中使用它。 And one more table Item3 of same Item.java class for third fragment. 第三个片段的同一个Item.java类的另一个表Item3。

One solution is to create another class Item2.java and Item3.java and extend Item.java to it and create the DAO and Room Database for that class. 一种解决方案是创建另一个类Item2.java和Item3.java,并将Item.java扩展到该类,然后为该类创建DAO和房间数据库。 But it would be redundant. 但这将是多余的。

Question :- 问题 :-

Is there any other way i can create multiple tables of same class having no relation to each other? 我还有其他方法可以创建彼此不相关的相同类的多个表吗?

Why don't you add another column in the Item table indicating the fragment, and index the table on that column? 为什么不在Item表中添加另一列指示片段,并在该列上建立索引呢? This way, all your data will be stored in the same table, at the same time differentiated by the new column, and the index will ensure that you get faster retrievals for any of the fragment. 这样,您的所有数据将被存储在同一张表中,同时以新列进行区分,并且索引将确保您可以更快地检索任何片段。

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

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