java - Play Framework 2.2 : How to define an ebean ManyToMany query

标签 java sql playframework ebean playframework-2.2

我正在尝试在 play 2.0 中实现 play 1.0 中的 yabe 教程

目前我陷入了标记功能:http://www.playframework.com/documentation/1.2.3/guide6

本质上问题是这样的:

我有一个 Post 类,每个 Post 对象可以有多个与该类关联的标签:

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

我想编写一个函数,给定一个标签数组,当且仅当所有标签都存在于 Post 对象中时,该函数才会返回一个 Posts 列表。

单元测试如下:

    @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();
}

我不知道下一步该做什么,因为我强行这样做却一事无成:(

谢谢!

最佳答案

尝试打开 EBean SQL 日志记录。对于查看 EBean 正在执行哪些 SQL 语句非常有帮助。

参见this Stackoverflow question

关于java - Play Framework 2.2 : How to define an ebean ManyToMany query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19339721/

相关文章:

java - JSF UIComponents 的更新值

java - 如何使用 spring 和mockito 获取被测对象的字段

sql - 循环可以锁自己吗?

sql - 是否可以在表之间创建关系?

eclipse - 是否有与 Spring Source Tools Suit 兼容的 scala 插件?

java - [spark-cassandra-connector]如何在spark 2.3.1中将scala隐式支持的代码转换为java

python - Django 分组依据

playframework - JHAML 和 Play Framework ?

playframework - 在 Play 框架中,何时使用 Promise 来处理请求,何时不使用?

java - 如何将数组追加到已经有数组的json文件中?