3

How do you persist an HashMap in greenDAO and how do you generate the respective entities?

I have read the documentation twice going forward and backward but nothing there. Google wasn't of any help either.

doplumi
  • 2,938
  • 4
  • 29
  • 45

1 Answers1

4

You should create an Entity with a String-primary-key and a String-proerty for the value:

Entity mapEntity = schema.addEntity("Map");
mapEntity.addStringProperty("key").primaryKey();
mapEntity.addStringProperty("value");

Maybe some other attributes for the properties are needed (depending on your needs) like unique, notNull.

If you want to store your Map inside an entity, that's not quite as simple:

Basically you create an entity like this for storing all Maps:

Entity mapEntity = schema.addEntity("Map");
mapEntity.addLongProperty("id").primaryKey().autoIncrement();
mapEntity.addStringProperty("key").unigue().notNull();
mapEntity.addStringProperty("value");

and then create a relation toOne() or toMany() to reference the corresponding map.

P.S. Maybe you should choose other names than key and value. These variable names are used often and may produce conflicts in greendao.

AlexS
  • 5,295
  • 3
  • 38
  • 54