我正在使用MongoDb和Morphia为我的应用编写后端,但是我在保存嵌入对象方面遇到了一个有趣的问题。
让我们说我正在撰写论坛软件。有帖子(父对象)和评论(子对象)。每个帖子都包含一个注释的ArrayList。我可以发一个帖子,然后添加一条评论。这很好。如果我尝试添加第二个Comment,看起来我的Morphia Query在搜索Post以附加Comment时会返回一个null对象。
这是代码,它在我的processComment方法中:
// Get Owner and ParentObjectId from the incoming JSONObject.
String commentOwner = jsonObject.getString("owner");
String parentObjectUuid = jsonObject.getString("parentObjectUuid");
// Create a new Comment from these values, and add the body.
Comment comment = new Comment(commentOwner, parentObjectUuid);
comment.setBody(jsonObject.getString("body"));
// Get Parent Object from Datastore.
Query<Post> query = datastore.createQuery(Post.class);
query.field("uuid").equalIgnoreCase(parentObjectUuid);
Post parentObject = query.get(); // <--- This is Line #144 where I think it fails.
// Add comment to its ParentObject, and save in our datastore.
parentObject.addComment(comment);
datastore.save(parentObject);
这是stacktrace:
java.lang.RuntimeException: java.lang.NullPointerException
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:76)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:850)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:282)
at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:193)
at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134)
at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146)
at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117)
at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:236)
at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:227)
at IncomingProcessor.processComment(IncomingProcessor.java:144)
at IncomingProcessor.processIncomingJson(IncomingProcessor.java:78)
at IncomingProcessor.<init>(IncomingProcessor.java:50)
at Main.main(Main.java:78)
Caused by: java.lang.NullPointerException
at org.mongodb.morphia.Key.hashCode(Key.java:149)
at java.util.HashMap.hash(HashMap.java:338)
at java.util.HashMap.put(HashMap.java:611)
at org.mongodb.morphia.mapping.cache.DefaultEntityCache.notifyExists(DefaultEntityCache.java:82)
at org.mongodb.morphia.mapping.cache.DefaultEntityCache.putEntity(DefaultEntityCache.java:90)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:293)
at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:210)
at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:144)
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:47)
... 12 more
为了清楚起见,这段代码第一次运行。当我尝试添加第二条评论时,它会失败。 有任何想法吗?感谢。
答案 0 :(得分:0)
Morphia正在重新保存整个Object,它改变了MongoDB用来识别Object的内部哈希码。解决问题的代码如下:
// This gets the Post parent object from the DB.
Query<Post> query = datastore.createQuery(Post.class).field("uuid").equal(objectUuid);
// This save the newComment to an ArrayList<Comment> called commentsList.
datastore.update(query, datastore.createUpdateOperations(Post.class).push("commentsList", newComment));
这让我发疯了。我使用的是datastore.save()方法,这是错误的。我需要使用.push()方法在CreateUpdateOperations上使用datastore.update()方法。
到此有很多诅咒。不确定我喜欢Morphia。