Play Framework - How to Query a Model using 'I

2019-04-11 20:39发布

How can I get the same results in Play Framework's Model Queries?

SELECT * FROM MyModel WHERE status IN (1, 5, 8) ORDER BY createdAt;

2条回答
爷的心禁止访问
2楼-- · 2019-04-11 21:04

If (1, 5, 8) is a constant, it's pretty straightforward:

List<MyModel> r = MyModel.find("status in (1, 5, 8) order by createdAt").fetch();

If it should be a parameter:

List<Integer> s = Arrays.asList(1, 5, 8);
List<MyModel> r = MyModel.find("status in :s order by createdAt")
    .bind("s", s).fetch();

An important point here is that you can use in clause with named parameters only, not with positional (?) ones, due to limitation of Hibernate.

查看更多
Rolldiameter
3楼-- · 2019-04-11 21:26

Have you tried

MyModel.find("status IN (:ids) ORDER BY createdAt").bind("ids", idExs).fetch();
查看更多
登录 后发表回答