我有一个与此类似的 Morphia 架构:
@Entity
class BlogEntry {
@Embedded
List<BlogComment> comments
}
@Embedded
class BlogComment {
String content
Long authorId
}
(以上代码仅供说明)
我正在尝试获取特定的 BlogComment 以便用新内容更新它。我有相应的 BlogEntry 对象可用,并且有authorId,就这个问题而言,这两个对象一起足以唯一地标识正确的 BlogComment。
我的问题是,BlogComment 没有显式包含对其“父”BlogEntry 对象的引用,那么如何编写吗啡查询来检索此 BlogComment?就像是:
//fetch the unique comment corresponding to this blog entry and this author ID.
BlogComment comment = ds.find(BlogComment.class, "blogEntryId =", blogEntry.id)
.filter("authorId", authorId)
.get();
既然您已经有了博客条目对象,为什么不使用简单的 Java 循环来过滤它呢?
@Entity
class BlogEntry {
@Embedded
List<BlogComment> comments
public BlogComment findCommentByAuthorId(String authorId) {
if (null == authorId) return null;
for (BlogComment comment: blogEntry.comments) {
if (authorId.equals(comment.authorId) return comment;
}
return null;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)