question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem when inserting object with hashmap which keys contain a dot

See original GitHub issue

Re-opens #268

I encountered this issue as well and reproduced it :

static class MyObjectWithMap {
        Map<String, String> map = Maps.newHashMap();
}
MyObjectWithMap obj = new MyObjectWithMap();
obj.map.put("keyOk", "OK");
getDs().save(obj); // No problem
obj.map.put("key.notOK", "notOK");
getDs().save(obj); // Problem

Generates this :

java.lang.IllegalArgumentException: Invalid BSON field name key.notOK
     at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)
     at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:219)
     at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:198)
     at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128)
     at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
     at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:48)
     at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:27)
     at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
     at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
plaacommented, Jan 20, 2016

Is there any generic workaround? We’re storing a document with generic JSON content (i.e. we do not control the key names). I’m thinking of having some pre/postprocessor that replaces invalid keys with an encoded version and replaces them upon reading. Maybe defining a special JsonMap class which would know to do the proper processing.

0reactions
evanchoolycommented, Jan 20, 2016

Not directly because embedded objects don’t have a lifecycle separate from the enclosing type. If you’d like help with the listeners, please post your questions to the mailing list https://groups.google.com/forum/#!forum/morphia.

--------------------------------

{ *name : *“Justin Lee”, * title : *“Software Engineer, twitter : "@evanchooly http://twitter.com/evanchooly_"_, web : [ “mongodb.com http://www.mongodb.com”, antwerkz.com ], location : New York, NY }

On Wed, Jan 20, 2016 at 2:25 PM, plaa notifications@github.com wrote:

Can any of the lifecycle methods (either annotations of EntityListeners) be used on embedded objects? I’d like to create JsonMap class, which functions as a map within an entity, but performs the transformation on the map automatically. Can I override that an embedded JsonMap object is serialized using a specific listener?

— Reply to this email directly or view it on GitHub https://github.com/mongodb/morphia/issues/827#issuecomment-173332352.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does a Java HashMap handle different objects with the ...
get(key): HashMap uses Key Object's hashcode to find out bucket location and then call keys.equals() method to identify correct node in LinkedList and...
Read more >
HashMap containsKey() Method in Java - GeeksforGeeks
The java.util.HashMap.containsKey() method is used to check whether a particular key is being mapped into the HashMap or not.
Read more >
How HashMap works in Java - Javarevisited
In hashing, hash functions are used to link keys and values in HashMap. Objects are stored by the calling put(key, value) method of...
Read more >
Using a Custom Class as a Key in a Java HashMap | Baeldung
A Map doesn't allow duplicate keys, so the keys are compared to each other using the Object#equals(Object) method. Because this method has ......
Read more >
How to use User defined Object as key in HashMap with an ...
Please check out my blog(http://learnsimple.in) for more technical videos.For any java/full stack/DevOps/developer/lead positions related ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found