0

该框是自动递增的。

假设我有一个像这样的对象:

@HiveType(...)
class Dummy {
  
  @HiveField(0)
  int id;

  @HiveField(1)
  String name;

}

我希望 Hive 反序列化将Dummy对象的键映射到id字段。

4

1 回答 1

2

如果你从 HiveObject 继承你的类,那么你可以使用内置属性'key'。

    @HiveType(...)
    class Dummy extends HiveObject{
      //id not needed anymore
   
      @HiveField(0)
      String name;
    
    }
    
    ...
    
    Box<Dummy> db = Hive.box<Dummy>('dummy');
    Dummy dummy = Dummy(name: "some text");
    db.add(dummy); //add assigns next integer as key

    //after storing object to a database, you can access its key using 'key' property
    int id = dummy.key;

    //another advantage of extending HiveObject, is that you can store or delete object from the database without accessing the box:
    dummy.name = "other text";
    dummy.save(); //put modified object into box
    dummy.delete(); //delete object from the database
于 2021-03-30T19:07:36.657 回答