我有一个具有多关系和函数的简单模型,呈现模型页面,如下所示。我想知道是否有一种方法可以按类别(ManytoMany 字段)过滤项目页面,例如 .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);
}
}
最佳答案
那是similar question just a few topics ago您可以使用其中的示例(还可以检查 relation filtering 的其他可能性)
您的查询应如下所示(查找包含名称中包含“some”单词的Categories
的Items
):
find.where()
.ilike("categories.name", "%some%")
.orderBy(sortBy + " " + order)
.findPagingList(pageSize)
.getPage(page);
关于java - 在 Play 中通过多对多关系过滤模型对象! 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13545416/