Im using ORMLite in my Android app. I need to persist this class, which has a HashMap. What is a good way of persisting it? Its my first time trying to persist a HashMap, also first time with ORMLite so any advice would be greatly appreciated!
*Edit*
If that makes any difference, the Exercise class is simply a String (that also works as id in the database), and the Set class has an int id (which is also id in database), int weight and int reps.
@DatabaseTable
public class Workout {
@DatabaseField(generatedId = true)
int id;
@DatabaseField(canBeNull = false)
Date created;
/*
* The hashmap needs to be persisted somehow
*/
HashMap<Exercise, ArrayList<Set>> workoutMap;
public Workout() {
}
public Workout(HashMap<Exercise, ArrayList<Set>> workoutMap, Date created){
this.workoutMap = workoutMap;
this.created = created;
}
public void addExercise(Exercise e, ArrayList<Set> setList) {
workoutMap.put(e, setList);
}
...
}
Wow. Persisting a
HashMapwhose value is aListofSets. Impressive.So in ORMLite you can persist any
Serializablefield. Here’s the documentation about the type and how you have to configure it:So your field would look something like:
Please note that if the map is at all large then this will most likely not be very performant. Also, your
Exerciseclass (and theListandSetclasses) need to implementSerializable.If you need to search this map, you might consider storing the values in the
Setin another table in which case you might want to take a look at how ORMLite persists “foreign objects”.