[2.2] : Porting Yabe 1.x tutorial to 2.2: ManyToMany query

20 views
Skip to first unread message

Marc Haubenstock

unread,
Oct 14, 2013, 6:28:03 AM10/14/13
to play-fr...@googlegroups.com
I am trying to implement the yabe tutorial from play 1.0 in play 2.0

Currently I am stuck at the tagging functionality: http://www.playframework.com/documentation/1.2.3/guide6

Essentially the problem is this:

I have a Post class, each Post object can have multiple number of tags associated with that class:

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


I want to write a function which, given an array of tags, will return a list of Posts, if and only if all tags are present in the Post objects.

The unit tests are as follows:

        @Test
   
public void testTags() {
       
// Create a new user and save it
       
SuperUser.setInstance("b...@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();


   
}


My current implementation is as follows:

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


I have no idea what to do next, as I am brute forcing this and getting nowhere :(

Thanks!
Reply all
Reply to author
Forward
0 new messages