简体   繁体   English

房间迁移?

[英]Room Migration?

Shall we describe all fields of a new table while migration from version X to X+1 like it is shown in the doc:我们应该在从版本X迁移到X+1描述新表的所有字段,如文档中所示:

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
            + "`name` TEXT, PRIMARY KEY(`id`))");
}
};

If so, how describe migration for the class with Embedded annotation with nested Embedded annotation?如果是这样,如何使用带有嵌套的Embedded注释的Embedded注释来描述class migration

public class DirectorsStaff {
private String secretKey;
@Embedded
private DirectorsAnswer directorsAnswer;

public String getSecretKey() {
    return secretKey;
}

public void setSecretKey(String secretKey) {
    this.secretKey = secretKey;
}

public DirectorsAnswer getDirectorsAnswer() {
    return directorsAnswer;
}

public void setDirectorsAnswer(DirectorsAnswer directorsAnswer) {
    this.directorsAnswer = directorsAnswer;
}
}

DirectorsStaff is also Embedded DirectorsStaff Embedded也被Embedded

Using @Embedded to annotate a field of an entity / pojo will actually create such subfields in the table.使用@Embedded来注释实体/pojo 的字段实际上会在表中创建这样的子字段。

Room just does you a favor to assemble them to a specific type whenever you do a query having such subfields.每当您执行具有此类子字段的查询时, Room都会帮助您将它们组合成特定类型。

Therefore you should describe all fields of a new table while migration.因此,您应该在迁移时描述新表的所有字段。


Suppose the database has a Foo table for version 1 and you want to add a new table Bar for version 2.假设数据库有一个版本 1 的Foo表,而您想为版本 2 添加一个新表Bar

@Entity
data class Foo(
  @PrimaryKey val id: Int,
  val name: String
)

@Entity
data class Bar(
  @PrimaryKey val id: Int,
  @Embedded(prefix = "foo_") val foo: Foo
)

class Migration1_2: Migration(1, 2) {
  override fun migrate(database: SupportSQLiteDatabase) {
     database.execSQL("CREATE TABLE Bar (id INTEGER PRIMARY KEY NOT NULL, foo_id INTEGER NOT NULL, foo_name TEXT NOT NULL)")
  }
}

migrate() should run ALTER TABLE (it's the main purpose, where one does not have to describe whole tables, but only describes the fields or indexes, which shall be altered), as well as occasionally it might contain a CREATE TABLE or a DROP TABLE statement. migrate()应该运行ALTER TABLE (它的主要目的是不需要描述整个表,而只描述字段或索引,它们将被更改),有时它可能包含一个CREATE TABLE或一个DROP TABLE语句。 what you've annotated as @Embedded , that should be another Room entity (this barely influences the migration, but the mapping).您注释为@Embedded内容应该是另一个 Room 实体(这几乎不会影响迁移,但会影响映射)。

Another approach using ColumnInfo , then you can directly reference that column使用ColumnInfo另一种方法,然后您可以直接引用该列

@Entity
data class Foo(
  @ColumnInfo(name = "foo_id") val id: Int,
  @ColumnInfo(name = "foo_name") val name: String
)

@Entity
data class Bar(
  @PrimaryKey val id: Int,
  @Embedded val foo: Foo
)

class Migration1_2: Migration(1, 2) {
  override fun migrate(database: SupportSQLiteDatabase) {
     database.execSQL("CREATE TABLE Bar (id INTEGER PRIMARY KEY NOT NULL, foo_id INTEGER NOT NULL, foo_name TEXT NOT NULL)")
  }
}

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

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