I have two entities, let's call them Alpha and Beta.
There is a one to many relationship between them, such that Beta has a foreign key to Alpha, MyAlphaId, and thus a reference property MyAlpha.
I have a predicate being built for Alphas.
e.g.
var predicateAlpha = new Predicate('name', 'contains', 'somevalue');
I want to then "transpose" this onto a query for Beta's where its myAlpha property matches that predicate.
e.g.
var predicateBeta = new Predicate('myAlpha', 'in', predicateAlpha);
I am aware of the ability for me to construct the predicate in the following manner:
var predicateBeta = new Predicate('myAlpha.name', 'contains', 'somevalue');
My issue is that I don't know what that predicate may be. I can't just pre-pend 'myAlpha.' as it may include an OData function such as 'concat('somefield', 'someotherfield')'.
Any other thoughts or suggestions on how I can achieve this?
For me, it would make sense for IN to accept a predicate that allows this to happen... I can't see the source code accepting a predicate so I can't think how else to make this happen...
1 Answer
Assuming that Alpha has a property betas, you could query for Alphas using the predicate, and expand the Betas so that they are brought in too. Then collect the Betas if you need them in a single list:
var query = em.createQuery(predicateAlpha).expand('betas');
var betas = [];
em.executeQuery(query).then(queryResult => {
queryResult.results.forEach(alpha => {
betas = betas.concat(alpha.betas);
});
});