While retrieving a list of objects, I want to filter the results based on another table which I only have the ID of.The objects are NOT linked in the ORM-model, but instead just contain a UUID.
Ie:
@Entity
class A {
@Id
private UUID id;
private UUID refB; // links to B
}
@Entity
class B {
@Id
private UUID id;
private boolean visible;
}
I want to retrieve all A’s where B.hidden is false OR where B doesn’t exist.
In SQL I would do something like
SELECT t0.* FROM a_table t0 LEFT JOIN b_table t1 ON (t0.ref_b = t1.id)
WHERE t1.hidden IS NULL OR t1.hidden = 0;
The reason I’m not just using RawSql is that I cannot find any way to use wildcards in the select, so all the attributes would have to be both maintained and manually added in the select.
I have also tried
List<A> listA = Ebean.find(A.class).where()
.join("LEFT JOIN b_table t1 ON (t0.ref_b = t1.id)")
.where().in("t1.hidden", "0", "NULL");
But then I get an error because WHERE is put before “LEFT JOIN”.
I assume that the “right” way would be to replace “private UUID refB” with “private B refB”. But doing that would make it easier to circumvent certain security measures.
Is this possible or will I have to add all attributes in a RawSql?
Filtering in Ebean is for other purposes than just manipulating the
WHEREclause, look at the comparison in other question.Here you have a proper way (I changed models to show complete, working sample)
Models
Controller
Result SQLs of H2
Alternatively you can also use SqlQuery to fetch just a SqlRows (not objects) check linked APIs for samples of usage, setting named parameters etc.: