Room数据库中具有相同对象类型的多个表 [英] Multiple tables with same type of objects in Room database

查看:1679
本文介绍了Room数据库中具有相同对象类型的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Room作为该应用程序的数据库.我有一种情况,其中某种类型的Object需要存储在单独的表中.例如,以Object称为Book.java

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

现在,我要有两个SQL表:

Now, I want to have two SQL tables:

  • Books_Read
  • Books_To_Read

请忽略SQL DB的任何命名约定-这只是一个示例

问题

通常,仅在Book.java类中使用@Entity(tableName = "Books_Read")并拥有一个将使用该表名的DAO类.

Normally, one would just use @Entity(tableName = "Books_Read") in the Book.java class and have a DAO class that will use that table name.

问题是;那么我将如何使用相同的Book.java类存储在Books_To_Read表中?由于我已经将@Entity(tableName = "Books_Read")定义为Book.java类的一部分,并且看不到在哪里为Book.java类定义Books_To_Read

The thing is; how would I then be able to use the same Book.java class to store in the Books_To_Read table? Since I already defined @Entity(tableName = "Books_Read") as part of the Book.java class and I see no where to define the Books_To_Read table for the Book.java class

我唯一能想到的解决方案是创建一个新类,这似乎有点骇人听闻和烦恼,让我们称呼它为BookToRead.java,它扩展了Book.java并在该类中定义了@Entity(tableName = "Books_To_Read").

The only solution I was able to come up with, which seems a little hackery and unnessasery, was to create a new class - let's call it BookToRead.java that extends Book.java and define @Entity(tableName = "Books_To_Read") in the class.

问题

是否有更好的方法来执行此操作,或者这是处理该问题的预期方法?

Is there a better way to do this or is this the expected way to handle it?

推荐答案

这是处理它的预期方法吗?

Is this the expected way to handle it?

.这是错误的方法.您应出于以下几个原因消除数据重复.

No. It is a wrong approach. You should eliminate duplication of data for several reasons.

  • 这将占用存储空间.随着数据库大小的增加,这 会变得更糟.

  • It will cost storage space. As the size of database increases, this will began to become worse.

这会使系统变得复杂.如果您要更新一个 字段,您可能必须在不同的位置执行相同的操作. 如果您错过其中任何一个,则整个数据将变为 不一致.可以执行以下操作 一次交易.但是保持数据库结构总是更好 干净.

It will make the system complicated. If you are updating a single field, You may have to perform the same action at different places. If you miss any one of them, the entire data will become inconsistent. It is possible to perform such operations as a single transaction. But it is always better to keep the database structure clean.


方法1:引入新表

您只能将书籍的详细信息存储在一个表中,例如"Books". "Books_To_Read"或任何其他表应仅包含对"Books"表的引用(通过使用"Books"表中的id/主键).然后,您可以使用JOIN关键字在单个查询中获取整个记录.

You can store details of books in only one table say "Books". "Books_To_Read" or any other table should contain only the reference to the "Books" table (By using the id/ primary key in "Books" table). You can then use the JOIN keyword to get the entire record at a single query.

如果每种类型(在这种情况下为已读和未读书籍)都有自己的字段集(例如,类似于已读日期,已读书籍的已读时间和已未读书籍的wish_to_read ),则首选此方法.

This method is preferred if each type (read and unread books in this case) has it's own set of fields (Like the read_date, read_time for read books and wish_to_read for unread books).

方法2:引入新字段

您可以简单地添加一个指定类型的新类型.在这种情况下,只有两种类型(读和未读),一个布尔型字段(如is_read之类)就可以了.

You can simply add a new type which specifies the type. In this case, there are only two type(read and unread), a boolean field (something like is_read) will be fine.

这将是最简单的方法.但这仅在您只想指出该行所属的类型时才有效.如果确实需要存储类型特定的数据,并且为此目的引入了其他字段,请记住,这些字段也将用于其他类型.

This would be the easiest method. But this will work only if you just want to indicate which type the row belongs to. If you do need to store type specific data and you introduce additional fields for that purpose, remember that those fields will exists for others types too.

这篇关于Room数据库中具有相同对象类型的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆