2

私はこれに似たモルヒネスキーマを持っています:

@Entity
class BlogEntry {
    @Embedded
    List<BlogComment> comments
}

@Embedded
class BlogComment {
    String content
    Long authorId
}

(説明のために上記のコード)

新しいコンテンツで更新するために、特定のBlogCommentを取得しようとしています。対応するBlogEntryオブジェクトが利用可能であり、authorIdがあります。これは、この質問の目的のために、これら2つを合わせて正しい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(); 
4

1 に答える 1

3

既にブログ エントリ オブジェクトがあるので、単純な 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;
    }

}
于 2012-01-17T23:08:53.497 に答える