my2iu / Jinq

LINQ-style queries for Java 8
Other
660 stars 71 forks source link

Join on with concat #78

Closed AmitaiMazliah closed 5 years ago

AmitaiMazliah commented 5 years ago

so I'm trying to build a query like this: SELECT COUNT(DISTINCT(a.id)) FROM articles a JOIN nlsproperties n ON varKey = CONCAT('article.subtitle.', a.article_subtitle, '.name') WHERE (title LIKE '%test%' OR varValue LIKE '%test%')

here is my code: stream.streamAll(getCurrentSession(), Article.class) .leftOuterJoin( (article, source) -> source.stream(I18n.class), (article, i18n) -> ("article.subtitle." + article.getArticleSubtitle().getId() + ".name").equals(i18n.getKey()) ) .where(pair -> JPQL.like(pair.getOne().getTitle(), "%" + text + "%") || JPQL.like(pair.getTwo().getValue(), "%" + text + "%")) .select(Pair::getOne).distinct().count();

And I'm getting the following error: Could not analyze lambda code

any ideas?

my2iu commented 5 years ago

You need to provide a full stack trace to see what the error is. Jinq will report the exact details of the problem later in the stack trace.

It's possible that concat is broken. I think Oracle was dabbling in new ways to implement string concatenation in Java, but I haven't been following along to see if Jinq needs any changes to handle it.

my2iu commented 5 years ago

Oh wait, "Could not analyze lambda code" means that something referred to in the lambda isn't serializable. I'm not sure which lambda it could be, but it looks like you're using a query parameter text. Are you sure that "text" is a local variable? Jinq only allows local variables as query parameters.

AmitaiMazliah commented 5 years ago

I'm not sure where the problem was but I was able to solve it by changing the code to: stream.streamAll(getCurrentSession(), Article.class) .leftOuterJoin( (article, source) -> source.stream(I18n.class), (article, i18n) -> article.getArticleSubtitle().getName().equals(i18n.getKey()) ) .where(pair -> JPQL.like(pair.getOne().getTitle(), "%" + text + "%") || JPQL.like(pair.getTwo().getValue(), "%" + text + "%")) .select(Pair::getOne).distinct().count();

my2iu commented 5 years ago

Great!