如何使用 Jinq 搜索 Set
How to search Set using Jinq
我的实体 class 中有集合 Set<Tag>
。 Tag
class 仅包含 Long id
和 String
值。我试图通过 Tag
找到 Place
但我收到错误 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()
我不熟悉你的模式,但也许你可以做一个连接来展平你的数据结构,像这样:
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);
我的实体 class 中有集合 Set<Tag>
。 Tag
class 仅包含 Long id
和 String
值。我试图通过 Tag
找到 Place
但我收到错误 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()
我不熟悉你的模式,但也许你可以做一个连接来展平你的数据结构,像这样:
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);