1

I have collection Set<Tag> in my entity class. Tag class contains only Long id and String value. Im trying to find Place by Tag but im getting error Could not analyze lambda code

    String name = places.getTag().getName();
    if (name != null) {
        stream = stream.where(p -> p.getTags().iterator().next().getName().equals(name));
    }

There is way to make it tight and elegant? I know that my code is incorrect, and im getting error because Jinq probably doesn't support something like this p.getTags().iterator().next().getName()

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58

1 Answers1

1

I am not familiar with you schema, but maybe you could do a join to flatten your data structures with something like this:

stream
   .joinList( p -> p.getTags() )
   .where( pair -> pair.getTwo().getName().equals(name) );

Again, it depends on your database schema and entities, but if you have the right relationships setup, then you could go even simpler with something like this:

tagStream
   .where( tag -> tag.getName().equals(name) )  // get the right tag
   .selectAllList( tag -> tag.getPlaces() )     // get all places with that tag

If you can't use a join though, you can try using a subquery, but subqueries behave a bit finnicky on different JPA providers. For subqueries, just make sure the subquery is in the same format as a normal query.

stream = stream
   .where(p -> JinqStream.from(p.getTags())
      .where( tag -> tag.getName().equals(name) )
      .count() > 0);
Ming-Yee Iu
  • 844
  • 6
  • 5