1

play 1.0 の矢部チュートリアルを play 2.0 に実装しようとしています。

現在、タグ付け機能で立ち往生しています: http://www.playframework.com/documentation/1.2.3/guide6

本質的に問題はこれです:

私は Post クラスを持っています。各 Post オブジェクトは、そのクラスに関連付けられた複数のタグを持つことができます:

@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> tags;

すべてのタグが Post オブジェクトに存在する場合にのみ、タグの配列を指定すると、Post のリストを返す関数を書きたいと思います。

単体テストは次のとおりです。

    @Test
public void testTags() {
    // Create a new user and save it
    SuperUser.setInstance("bob@gmail.com", "secret", "Bob").save();

    SuperUser bob = SuperUser.getInstance();

    // Create a new post
    Post bobPost = new Post(bob, "Hello world","My first post");
    bobPost.save();

    Post anotherBobPost = new Post(bob, "Hello world", "Hop");
    anotherBobPost.save();

    // Well
    assertEquals(0, Post.findTaggedWith("Red").size());

    // Tag it now
    bobPost.tagItWith("Red").tagItWith("Blue").save();
    anotherBobPost.tagItWith("Red").tagItWith("Green").save();

    // Check
    assertEquals(2, Post.findTaggedWith("Red").size());
    assertEquals(1, Post.findTaggedWith("Blue").size());
    assertEquals(1, Post.findTaggedWith("Green").size());

    // Checks for multiple tag params
    assertEquals(1, Post.findTaggedWith("Red", "Blue").size()); //Fail -  Actual: 0
    assertEquals(1, Post.findTaggedWith("Red", "Green").size());
    assertEquals(0, Post.findTaggedWith("Red", "Green", "Blue").size());
    assertEquals(0, Post.findTaggedWith("Green", "Blue").size());

    SuperUser.removeSuperUser();

}

私の現在の実装は次のとおりです。

    public static List<Post> findTaggedWith(String... tags) {

    ExpressionList<Post> expAcc = Post.find.fetch("tags").where().conjunction();

    for( String tag : tags){

        expAcc = expAcc.eq("tags.name", tag);

    }

    return expAcc.endJunction().findList();
}

私はこれをブルートフォースしてどこにも行かないので、次に何をすべきかわかりません:(

ありがとう!

4

2 に答える 2

1

EBean SQL ロギングを有効にしてみてください。EBean が実行している SQL ステートメントを確認するのに非常に役立ちます。

このStackoverflowの質問を参照してください

于 2013-10-16T13:38:56.667 に答える