简体   繁体   English

查找集合字段包含任何给定项的所有条目

[英]Finding all entries where a collection field contains any of the given items

I have a Spring Boot application that contains an entity like this (some fields stripped for compactness): 我有一个Spring Boot应用程序,它包含一个这样的实体(为了紧凑而剥离了一些字段):

@Entity
public class Message extends AbstractPersistable<Long> {
    @ManyToMany(targetEntity = Tag.class, fetch = FetchType.EAGER)
    private Set<Tag> tags;
    // getter and setter for tags here
}

Tag is just a simple entity with a name field. Tag只是一个带有名称字段的简单实体。


Now, I have another Set<Tag> in my code obtained from the user. 现在,我在我的代码中从用户那里获得了另一个Set<Tag> I want to find all Message s that have any of the tags in this set. 我想找到所有包含此集合中任何标记的Message For example, if we have the following messages: 例如,如果我们有以下消息:

ID    TAGS
1     1, 2, 3
2     2, 5, 7
3     2, 4, 7

Then the query should, given the set [3, 4] return messages 1 and 3. 然后,在给定[3, 4]返回消息1和3的情况下[3, 4]查询应该是。


I tried writing this repository: 我尝试编写这个存储库:

public interface MessageRepository extends JpaRepository<Message, Long> {
    List<Message> findByTags(Set<Tag> tag);
}

I enabled query logging and my code produced a query, which I've cleaned up a bit here. 我启用了查询日志记录 ,我的代码生成了一个查询,我在这里清理了一下。 The query produces no results in the cases I tried, and I have no idea what the scalar = array comparison is doing. 在我尝试的情况下,查询没有产生任何结果,我不知道scalar = array比较是做什么的。

SELECT message.id AS id FROM message
LEFT OUTER JOIN message_tags ON message.id = message_tags.message_id
LEFT OUTER JOIN tag ON message_tags.tags_id = tag.id
WHERE tag.id = (1,2,3,4,5) -- this is the input set

As suggested by @AliDehghani I tried writing the method as findByTagsIn(Set<Tag> tag) . 正如@AliDehghani所建议的,我尝试将该方法编写为findByTagsIn(Set<Tag> tag) This replaced the last = with in in the query. 这取代了最后=in查询中。

I got results, but there was another problem: the results were repeated for each matching tag as one might guess from the query. 我得到了结果,但还有另一个问题:对于每个匹配的标记重复结果,因为可能会从查询中猜到。 For example, searching for [2, 7] with the example messages above would return message 1, message 2 twice and message 3 twice. 例如,使用上面的示例消息搜索[2, 7]将返回消息1,消息2返回两次,消息3返回两次。

As far as I know adding some kind of GROUP BY clause might help here. 据我所知,添加某种GROUP BY子句可能对此有所帮助。


The predefined query method keywords don't seem to have any features related to this either, so I think I have to write my own using @Query or something else. 预定义的查询方法关键字似乎也没有任何与此相关的功能,所以我想我必须使用@Query或其他东西编写自己的。

I can't seem to figure out a query that would work either, and I'm not very experienced in H2 so I don't really want to guess how one might work either. 我似乎无法找出一个可以工作的查询,而且我在H2中不是很有经验,所以我真的不想猜测一个人如何工作。

I don't want to write a method that find all messages with a single tag, call it for each tag and combine the results, since that would be ugly and, given a lot of tags as input, very slow. 我不想编写一个方法来查找具有单个标记的所有消息,为每个标记调用它并组合结果,因为这将是丑陋的,并且,如果输入很多标记,则非常慢。 How should I write my query method? 我该如何编写查询方法?

List findByTags(Set tag); 列出findByTags(设置标签);

As you can see from the query log, this query method will only find messages that are tagged with all tags in the given tag parameter, which is not what you want. 从查询日志中可以看出,此查询方法仅查找使用给定tag参数中的所有标记标记的消息,这不是您想要的。

In order to find messages that are tagged with either of those tags, you can use the following query method: 要查找使用这些标记之一标记的邮件,可以使用以下查询方法:

List<Message> findByTagsIn(Set<Tag> tag);

I got results, but there was another problem: the results were repeated for each matching tag as one might guess from the query. 我得到了结果,但还有另一个问题:对于每个匹配的标记重复结果,因为可能会从查询中猜到。

In order to get rid of those repeated messages, you can fetch only distinct values: 为了摆脱那些重复的消息,你只能获取不同的值:

List<Message> findDistinctByTagsIn(Set<Tag> tag);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找集合字段包含所有给定项目的所有条目 - Finding all entries where a collection field contains ALL of the given items 查找列表字段包含dynamodb中值的所有项目 - find all items where a list field contains a value in dynamodb 查找字符串是否包含集合中的任何字符串 - Finding if a string contains any string in a collection 如何使用 Hamcrest 检查集合是否包含按给定顺序的项目 - How to check if collection contains items in given order using Hamcrest 使用包含jpql的给定集合的所有元素的集合查找项目 - Finding items with a set containing all elements of a given set with jpql 给定集合名称(ArrayList、LinkedList 等)和集合中的项目,如何创建任何集合? - How do I create any Collection given the collection name(ArrayList, LinkedList etc) and items in the collection? Hibernate:选择其中包含所有指定值的实体 - Hibernate: Select entities where collection contains all of the specified valus 如何检查列表中的所有项目是否包含字符串中的任何字符 - How to check that all items in the list contains any character from string Java:有一个项目集合,每个项目都有一个“ previousItem”字段,订购该集合的最有效方法是什么? - Java: having a collection of items, where each item has a field “previousItem”, what is the most efficient way to order the collection? 使用Arraylist查找给定的Integer是否在Array中包含() - Finding if given Integer is in Array or not using Arraylist contains ()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM