[英]Persist HashMap in ORMLite
Im using ORMLite in my Android app. 我在我的Android应用程序中使用ORMLite。 I need to persist this class, which has a HashMap.
我需要坚持这个有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!
这是我第一次尝试坚持HashMap,也是第一次使用ORMLite,所以任何建议都将非常感谢!
* 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. * 编辑 *如果这有任何区别,那么Exercise类只是一个String(在数据库中也作为id),而Set类有一个int id(在数据库中也是id),int weight和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
HashMap
whose value is a List
of Set
s. 持久化
HashMap
其值为Set
的List
。 Impressive. 令人印象深刻。
So in ORMLite you can persist any Serializable
field. 因此,在ORMLite中,您可以保留任何
Serializable
字段。 Here's the documentation about the type and how you have to configure it: 这是关于类型的文档以及如何配置它:
http://ormlite.com/docs/serializable
http://ormlite.com/docs/serializable
So your field would look something like: 所以你的领域看起来像:
@DatabaseField(dataType = DataType.SERIALIZABLE)
Map<Exercise, List<Set>> workoutMap;
Please note that if the map is at all large then this will most likely not be very performant. 请注意 ,如果地图非常大,那么这很可能不是很有效。 Also, your
Exercise
class (and the List
and Set
classes) need to implement Serializable
. 此外,您的
Exercise
类(以及List
和Set
类)需要实现Serializable
。
If you need to search this map, you might consider storing the values in the Set
in another table in which case you might want to take a look at how ORMLite persists "foreign objects" . 如果需要搜索此映射,可以考虑将值存储在另一个表中的
Set
中,在这种情况下,您可能需要查看ORMLite如何持久保存“外来对象” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.