[英]Is there a JPA / JPQL query to search for a subset of entities passed as JSON in spring?
I have two entities defined in spring and I want to query for a post which contains a list of given tags as a subset.我在 spring 中定义了两个实体,我想查询包含给定标签列表作为子集的帖子。
My two entites look like我的两个实体看起来像
public class Post {
private @Id Integer id;
private @Column String text;
private @OneToMany List<Tag> tags;
}
and和
public class Tag {
private @Id Integer id;
private @Column String key;
private @Column String value;
private @ManyToOne Post post;
}
I try to implement a query to search for posts containing a set of tags like我尝试实现一个查询来搜索包含一组标签的帖子,例如
@Query("SELECT pst FROM Post pst JOIN pst.tags tgs WHERE :tags MEMBER OF pst.tags")
List<Post> findByTags(@Param("tags") Tag[] tags);
And called it like URL/tags/search/findByTags?tags=[{"key":"test","value":"test"}]并称之为 URL/tags/search/findByTags?tags=[{"key":"test","value":"test"}]
My first problem is that I could not could not get it to accept tag contents instead of tag ids so I tried to solve it like this query我的第一个问题是我无法让它接受标签内容而不是标签 ID,所以我试图像这个查询一样解决它
@Query("SELECT post FROM Post pst JOIN post.tags WHERE (SELECT tg from Tag tg WHERE tg.key = :key AND tg.value = :value) MEMBER OF post.tag")
List<Post> findByTags(@Param("key") String key, @Param("value") String value);
And called it like URL/tags/search/findByTags?key=test&value=test which got me following error并称其为 URL/tags/search/findByTags?key=test&value=test 这让我遇到了以下错误
Scalar subquery contains more than one row标量子查询包含多于一行
Is there a way to solve both requirements?有没有办法解决这两个要求? It would be a bonus to be able to find all posts which contain a tag by key with any value.
能够通过具有任何值的键找到包含标签的所有帖子将是一个奖励。
Edit to clarify:编辑以澄清:
Imagine I have two posts which I will will sketch as JSON including relation for tags.想象一下,我有两个帖子,我将把它们绘制为 JSON,包括标签的关系。
[
{
id: 1,
text: "text1",
tags: [
{
id: 1,
key: "key1",
value: "value1",
},
{
id: 2,
key: "key2",
value: "value2",
},
{
id: 3,
key: "key3",
value: "value3",
},
],
},
{
id: 2,
text: "text2",
tags: [
{
id: 4,
key: "key1",
value: "value1",
},
{
id: 5,
key: "key2",
value: "value2",
},
],
},
]
And I would like to query for posts with a JSON including information of tags:我想用包含标签信息的 JSON 查询帖子:
[
{
key: "key1",
value: "value1",
},
{
key: "key2",
value: "value2",
}
]
So it is necessary that I can query for information of tags which describe a subset of tags in the post, eg more than one key and value pair without having to specify in the query how much parameters I will have and as bonus it would be nice to do all this by using JSON as parameter.因此,我有必要查询描述帖子中标签子集的标签信息,例如多个键和值对,而无需在查询中指定我将拥有多少参数,作为奖励,这会很好通过使用 JSON 作为参数来完成所有这些。 So for above query I would get an answer containing both posts.
所以对于上面的查询,我会得到一个包含两个帖子的答案。
If you want all posts by tag and value you can try this:如果您希望按标签和值显示所有帖子,您可以尝试以下操作:
@Query("SELECT pst FROM Post pst JOIN pst.tags tgs WHERE tags.key = :key AND tags.value = :value")
List<Post> findByTags(@Param("key") String key, @Param("value") String value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.