Ebean 中的过滤不仅用于操作WHERE
子句,还有其他目的,请查看其他问题中的比较。
在这里,您有一个正确的方法(我更改了模型以显示完整的工作示例)
楷模
@Entity
public class A extends Model {
@Id
public Integer id;
@ManyToOne
public B b;
public static Finder<Integer, A> find
= new Finder<Integer, A>(Integer.class, A.class);
}
@Entity
public class B extends Model {
@Id
public Integer id;
public Boolean hidden;
public static Finder<Integer, B> find
= new Finder<Integer, B>(Integer.class, B.class);
}
控制器
public static Result index() {
// Insert some data one every request
B bFalse = new B();
bFalse.hidden = false;
bFalse.save();
B bTrue = new B();
bTrue.hidden = true;
bTrue.save();
A a1 = new A();
a1.b = bFalse;
a1.save();
A a2 = new A();
a2.b = bTrue;
a2.save();
A a3 = new A();
a3.b = bTrue;
a3.save();
// Let's search...
List<A> aListOfNotHidden = A.find.where().and(Expr.eq("b.hidden", false), Expr.isNotNull("b.hidden")).findList();
for (A a : aListOfNotHidden) {
Logger.info("NOT hidden " + a.id);
}
List<A> aListOfHidden = A.find.where().eq("b.hidden", true).findList();
for (A a : aListOfHidden) {
Logger.warn("HIDDEN " + a.id);
}
return ok("check logs in your console");
}
H2的结果SQL
-- all NOT hidden
select t0.id c0, t0.b_id c1
from a t0
left outer join b t1 on t1.id = t0.b_id
where (t1.hidden = false and t1.hidden is not null )
-- all hiden
select t0.id c0, t0.b_id c1
from a t0
left outer join b t1 on t1.id = t0.b_id
where t1.hidden = true
或者,您也可以使用SqlQuery仅获取SqlRows(不是对象)检查链接的 API 以获取使用示例、设置命名参数等:
List<SqlRow> rows = Ebean.createSqlQuery("select t0.* " +
"from a t0 left outer join b t1 on t1.id = t0.b_id " +
"where (t1.hidden = false and t1.hidden is not null ) ").findList();
for (SqlRow row : rows) {
// do something with each row here, use methods such
// as getString("fieldname") for retrieving data
// (from SqlRow API)
}