如何使用 morphia 在 mongodb 中存储 HashMap?
如何使用 morphia 将 HashMap 存储在 mongodb 中?
我不确定这是否是吗啡的一个错误,或者我只是做错了。
说我有这个域模型
@Entity("person")
public class Person {
private String property1;
private String property2;
private HashMap<String, Thing> things;
}
,当我尝试使用扩展 BasicDAO repo.save(personInstance) 的类保存它时,我收到此错误:(UsedView 相当于上面示例中的 Thing)
java.lang.IllegalArgumentException:can't序列化类 com.model.Design.UsedView 在 org.bson.BSONencoder._putObjectField(BSONencoder.java:205) 在 org.bson.BSONencoder.putMap(BSONencoder.java:245) 在 org.bson.BSONencoder._putObjectField(BSONencoder.java:177) 在 org.bson.BSONencoder.putObject(BSONencoder.java:121) 在 org.bson.BSONencoder.putObject(BSONencoder.java:67) 在 com.mongodb.OutMessage.putObject(OutMessage.java:189) 在 com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:245) 在 com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209) 在 com.mongodb.DBCollection.insert(DBCollection.java:66) 在 com.mongodb.DBCollection.save(DBCollection.java:622) 在 com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:731) 在 com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:793) 在 com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:787) 在 com.google.code.morphia.dao.BasicDAO.save(BasicDAO.java:109)...
然后如果我更改:
"private HashMap<String, Thing> things"
它
"private HashMap<String, String> things"
可以很好地保存。
有什么想法吗?
非常感谢!
How do i store HashMaps in mongodb using morphia?
I'm not sure if this is a bug in morphia, or if i'm just doing it wrong.
say i have this domain model
@Entity("person")
public class Person {
private String property1;
private String property2;
private HashMap<String, Thing> things;
}
when i try to save this using a class that extends BasicDAO repo.save(personInstance) i get this error: (UsedView is the equivalent of Thing in the example above)
java.lang.IllegalArgumentException: can't serialize class com.model.designed.UsedView
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
at org.bson.BSONEncoder.putMap(BSONEncoder.java:245)
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:177)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
at com.mongodb.OutMessage.putObject(OutMessage.java:189)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:245)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
at com.mongodb.DBCollection.insert(DBCollection.java:66)
at com.mongodb.DBCollection.save(DBCollection.java:622)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:731)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:793)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:787)
at com.google.code.morphia.dao.BasicDAO.save(BasicDAO.java:109)...
Then if i changed:
"private HashMap<String, Thing> things"
to
"private HashMap<String, String> things"
it saves it fine.
any thoughts?
much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到了老问题,但我遇到了这个:(未经测试)
“...这可能包含 MongoDB 驱动程序支持的任何基本类型,包括列表和映射,但没有复杂的对象,除非您已向 Morphia 注册了转换器(例如
morphia.getMapper().getConverters().addConverter(new MyCustomTypeConverter())
。”来自:
http://www.carfey.com/blog/using-mongodb-with-吗啡/
正如所说:未经测试。
嗯。
old question I realize, but I came across this: (untested)
"...This could contain any basic types supported by the MongoDB driver including Lists and Maps, but no complex objects unless you have registered converters with Morphia (e.g.
morphia.getMapper().getConverters().addConverter(new MyCustomTypeConverter())
."From:
http://www.carfey.com/blog/using-mongodb-with-morphia/
As said: untested.
hth.
Thing
是否映射为 Morphia 实体?如果没有,吗啡将不知道如何将其保存到 mongo。Is
Thing
mapped as a Morphia entity? If not, morphia won't know how to save it to mongo.