Jinq icon indicating copy to clipboard operation
Jinq copied to clipboard

Add support to pass parameters in crossJoin

Open oberien opened this issue 7 years ago • 3 comments

Is possible / feasible to implement crossJoin in a way that allows passing parameters while cross-joining?

Given these methods:

public JPAJinqStream<Customer> getCustomer(long id) {
    return streams.streamAll(em, Customer.class)
        .where(c -> c.getId() == id);
}

public JPAJinqStream<Sale> getSales(List<Long> ids) {
    return streams.streamAll(em, Sale.class)
        .where(s -> JPQL.isInList(s.getId(), ids));
}

Would it be possible to implement a crossJoin taking a lambda which takes the current type as parameter and returns the new stream to join with like this?

getCustomer(1337)
    .crossJoin(c -> getSales(c.getSales()));

oberien avatar Mar 27 '17 17:03 oberien

If you need a lambda, you can just use the normal join() syntax. I think it would be something like this:

getCustomer(1337).join(
    (c, source) -> source.stream(Sale.class)
        .where(s -> JPQL.isInList(s.getId(), c.getSales()) );

my2iu avatar Mar 27 '17 19:03 my2iu

This case is highly simplified. In reality we are talking about both methods having 20+ lines of Jinq code. While I could copy the code and create a third method (which I will do if you say that this is not possible), it would be code duplication which I would like to avoid.

oberien avatar Mar 28 '17 10:03 oberien

It's not really possible since Jinq currently can't trace into other method calls when trying to decode a lambda.

You can maybe make a method that returns the lambda you need, but I'm not sure if that would work for your code. Something like

JoinWithSource<...> createJoin() {
   return (c, source) -> source.stream(Sale.class)
        .where(s -> JPQL.isInList(s.getId(), c.getSales())
}

getCustomer(1337).join( createJoin() );

my2iu avatar Mar 28 '17 16:03 my2iu