Problem serializing HashMap

71 views
Skip to first unread message

azlists

unread,
Mar 6, 2012, 9:06:28 AM3/6/12
to Morphia
Hi,

I'm trying to serialize an entity field which is a HashMap of which
the key is a URL string


ex :
@Entity("ffobar")
public class MyEntityFoo {

Map<String, Integer> myField = new HashMap<String,Integer>();

public void dostuff(){
myField.put("http://groups.google.com/group/morphia/post", 123);
}

}

The problem I get when saving the entity if mongodb throws an
exception because of the dots
"." contained in the URL string map key.

the error is :

java.lang.IllegalArgumentException: fields stored in the db can't
have . in them. (Bad Key: 'http://groups.google.com/group/morphia/
post')
at com.mongodb.DBCollection.validateKey(DBCollection.java:1213)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1196)
at com.mongodb.DBCollection._checkKeys(DBCollection.java:1186)
at com.mongodb.DBCollection._checkObject(DBCollection.java:1171)
at com.mongodb.DBCollection.save(DBCollection.java:718)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:772)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:834)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:828)
at com.google.code.morphia.dao.BasicDAO.save(BasicDAO.java:109)




What can I do ? Any way around this ?
I figured this accidentally but I think that I now need to check
everywhere I use Map with a String key to store data because this can
happen anywhere you use a String ...

Thanks for your feedback !

Alex



Ahmet Alp Balkan

unread,
Mar 6, 2012, 9:17:08 AM3/6/12
to mor...@googlegroups.com
Quick dirty hack:

Replace periods with commas before saving, and replace back after
restoring. If all your keys are URLs, URLs don't contain unencoded
commas. Maybe we can find a better workaround if you provide your use
case in detail.

--
Ahmet Alp Balkan <http://ahmetalpbalkan.com>

Sent from my iPad

Reply all
Reply to author
Forward
0 new messages