Mooh
Mooh

Reputation: 1247

Filtering Model objects by a ManytoMany relation in Play! 2.0

I have a simple model with a manytomany relation and a function, rendering Model Pages as you can see below. I want to know if there's a way to filter item pages by categories (ManytoMany field) something like .ilike("categories", "%" + filter + "%")

public class Item extends Model {
    @Id
    public Long id;

    @ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public List<Category> categories = new ArrayList<Category>();

    public String title;

    @Formats.DateTime(pattern="dd/MM/yyyy")
    public Date postDate=new Date();

    public String content;

    public String picture;

    public String price;

    public String url;

    public static Finder<Long,Item> find = new Finder<Long,Item>(
            Long.class, Item.class
          ); 


    public static Page<Item> page(int page, int pageSize, String sortBy, String order, String filter) {
            return
                find.where()
                    .ilike("content", "%" + filter + "%")
                    .orderBy(sortBy + " " + order)
                    .findPagingList(pageSize)
                    .getPage(page);
    }
}

Upvotes: 0

Views: 290

Answers (1)

biesior
biesior

Reputation: 55798

That was similar question just a few topics ago and you can use a sample from it (and also check other possibility for relation filtering)

your query should look like this (to find Items which contains Categories that contains 'some' word in their name) :

find.where()
        .ilike("categories.name", "%some%")
        .orderBy(sortBy + " " + order)
        .findPagingList(pageSize)
        .getPage(page);

Upvotes: 1

Related Questions