How to get key from Hive box object and map it to specific field of the object?
961
If you inherit your class from HiveObject, then you can use built in property '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
Author by
Mensch
Updated on December 20, 2022Comments
-
Mensch over 1 year
The box is auto-incrementing.
Let's say that I have an object like so:
@HiveType(...) class Dummy { @HiveField(0) int id; @HiveField(1) String name; }
I want Hive deserialization to map the
Dummy
object's key toid
field. -
Mensch about 3 yearsThe reply is much appreciated. I've switched to SQLite. I hope this helps someone else.