Spring 数据 jpa 存储库检索多个计数
Spring data jpa repository retrieve multiple counts
我有以下域实体:
Conversation,里面有一个Message的列表,每个都有一个集合newFor 参与者s。列表中的消息还没有为谁阅读。
我正在努力编写一个存储库接口方法(或查询)来检索包含该参与者的许多未读消息的对话图。
Atm 我使用以下方法仅检索对话列表:
@Query("SELECT c from Conversation c JOIN c.participants p WHERE :participant IN p")
List<Conversation> findByParticipantsIn(Participant participant, Pageable pageable);
但我需要以某种方式更新查询,以便能够包含嵌套的未读消息的计数。类似的东西:
@Query("SELECT c, count(..my problem is here..) from Conversation c LEFT JOIN c.participants p WHERE :participant IN p ")
List<Object[]> findByParticipantsIn(Participant participant, Pageable pageable);
有没有人知道如何将连接放入 count() 或者我还能在这里使用什么?
UPD
公开关系的实体片段:
public class Conversation {
...
@OneToMany(cascade = CascadeType.ALL)
private List<Message> messages = new ArrayList<>();
...
@ManyToMany(fetch = FetchType.EAGER)
private Set<Participant> participants = new LinkedHashSet<>();
...
}
public class Message {
...
@ManyToMany(fetch = FetchType.EAGER)
private Set<Participant> newFor = new HashSet<>();
...
}
Participant 不知道 Message 和 Conversation 存在。
最后,我可以通过这种方式实现调用单个存储库方法检索数据的目标:
@Query("SELECT c, (SELECT COUNT(m) FROM Message m WHERE m.conversation = c AND :participant MEMBER OF m.newFor) " +
"FROM Conversation c " +
"WHERE :participant MEMBER OF c.participants " +
"GROUP BY c, c.lastMessage.createdDate " +
"ORDER BY c.lastMessage.createdDate DESC")
List<Object[]> findConversationsPerParticipant(Participant participant, Pageable pageable);
此查询根据上述数据模型检索给定 participant
的对话以及未读消息的计数。
不确定这种方法是否足够有效,但我相信它可能比对远程数据库的 2 个单独调用更快。
我有以下域实体: Conversation,里面有一个Message的列表,每个都有一个集合newFor 参与者s。列表中的消息还没有为谁阅读。
我正在努力编写一个存储库接口方法(或查询)来检索包含该参与者的许多未读消息的对话图。
Atm 我使用以下方法仅检索对话列表:
@Query("SELECT c from Conversation c JOIN c.participants p WHERE :participant IN p")
List<Conversation> findByParticipantsIn(Participant participant, Pageable pageable);
但我需要以某种方式更新查询,以便能够包含嵌套的未读消息的计数。类似的东西:
@Query("SELECT c, count(..my problem is here..) from Conversation c LEFT JOIN c.participants p WHERE :participant IN p ")
List<Object[]> findByParticipantsIn(Participant participant, Pageable pageable);
有没有人知道如何将连接放入 count() 或者我还能在这里使用什么?
UPD
公开关系的实体片段:
public class Conversation {
...
@OneToMany(cascade = CascadeType.ALL)
private List<Message> messages = new ArrayList<>();
...
@ManyToMany(fetch = FetchType.EAGER)
private Set<Participant> participants = new LinkedHashSet<>();
...
}
public class Message {
...
@ManyToMany(fetch = FetchType.EAGER)
private Set<Participant> newFor = new HashSet<>();
...
}
Participant 不知道 Message 和 Conversation 存在。
最后,我可以通过这种方式实现调用单个存储库方法检索数据的目标:
@Query("SELECT c, (SELECT COUNT(m) FROM Message m WHERE m.conversation = c AND :participant MEMBER OF m.newFor) " +
"FROM Conversation c " +
"WHERE :participant MEMBER OF c.participants " +
"GROUP BY c, c.lastMessage.createdDate " +
"ORDER BY c.lastMessage.createdDate DESC")
List<Object[]> findConversationsPerParticipant(Participant participant, Pageable pageable);
此查询根据上述数据模型检索给定 participant
的对话以及未读消息的计数。
不确定这种方法是否足够有效,但我相信它可能比对远程数据库的 2 个单独调用更快。