简体   繁体   中英

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. 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

Now, I want to have three SQL tables:

Item1

Item2

Item3

ignore any naming conventions for SQL DB please - this is just an example

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.

I am having 3 fragments, each being independent of each other. I just want three database with same attributes and functions as Item. I want to create another table Item2 of same Item.java class to use it in the second fragment. And one more table Item3 of same Item.java class for third fragment.

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. 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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