如何使用Jinq搜索Set

时间:2015-09-07 14:11:25

标签: java spring hibernate jinq

我的实体类中有集合Set<Tag>Tag类仅包含Long idString值。我试图通过Place查找Tag,但我收到错误Could not analyze lambda code

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

有办法让它紧凑而优雅吗? 我知道我的代码不正确,我得到错误,因为Jinq可能不支持这样的p.getTags().iterator().next().getName()

1 个答案:

答案 0 :(得分:1)

我不熟悉您的架构,但也许您可以通过以下方式进行连接以展平您的数据结构:

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

同样,它取决于您的数据库架构和实体,但如果您有正确的关系设置,那么您可以更简单地使用以下内容:

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

如果您不能使用连接,则可以尝试使用子查询,但子查询在不同的JPA提供程序上表现得有点不合适。对于子查询,只需确保子查询的格式与普通查询的格式相同。

stream = stream
   .where(p -> JinqStream.from(p.getTags())
      .where( tag -> tag.getName().equals(name) )
      .count() > 0);