[英]Complex Sort with Spring Data JPA
我正在使用Spring數據規范和排序API(我的存儲庫是JpaSpecificationExecutor),並且我的數據模型如下所示(縮小了一點)
@Entity
public class Message extends AbstractVersionedEntity {
@OneToMany
private Set<MessageOwner> messageOwners = new HashSet<>();
}
@Entity
public class MessageOwner extends AbstractVersionedEntity {
@ManyToOne
private Mailbox owner;
@Enumerated(EnumType.STRING)
private MessageOwnerType type;
}
@Entity
public class Mailbox extends AbstractVersionedEntity {
@Column(unique=true)
private String ldapId;
}
我需要按收件人(即,按MessageOwnerType.TO類型的messageOwners的郵箱的ldapId排序)對消息進行排序。
message->messageOwners(type=TO)->owner->ldapId
此外,應該為每條消息對收件人進行排序-如果有多個messageOwner類型為MessageOwnerType.TO,那么應該按字母順序選擇第一個作為消息的排序值。
在純SQL中,我可能會使用某種子查詢來執行此操作。 使用Spring Data Specification&Sort對象是否有可能發生類似的事情? 除非必須,否則我將不必重寫QueryDSL / raw Criteria中的所有內容。
好的,所以我弄清楚了如何使用“規范”來實現它,但是它並不漂亮。 我無法使用Sort對象,而是使用JPA CriteriaBuilder將orderBy子句放在Specification中。
這實際上導致了SimpleJpaRepository的問題,該問題在所有可分頁的findAll查詢之前執行select count()。 我不得不禁用它(在Stackoverflow答案的幫助下。
所以我的規格看起來像這樣:(討厭,我知道。如果有人有更好的建議,我很想聽聽他們的建議)
@Override public Predicate toPredicate(Root<Message> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// Note - this would be simpler if JPA allowed subqueries in joins. As it doesn't,
// we have to put the subquery in a where clause, join both sides with the mailbox
// and correlate the results
// join the main query on the mailbox (for the ORDER BY clause)
Join<Message, MessageOwner> messageOwners = root.join("messageOwners", JoinType.LEFT);
Join<Message, Mailbox> mailbox = messageOwners.join("owner");
// create a subquery and correlate the messages with the main query
Subquery<String> subQuery = query.subquery(String.class);
Root<Message> sqMessage = subQuery.from(Message.class);
Root<Message> correlateMessage = subQuery.correlate(root);
// join the subquery on the mailbox
Join<Object, Object> sqMessageOwners = sqMessage.join("messageOwners", JoinType.LEFT);
Join<Message, Mailbox> sqMailbox = sqMessageOwners.join("owner");
// get the lowest ldapId alphabetically
Expression<String> minLdapId = cb.least(sqMailbox.<String>get("ldapId"));
// the actual subquery
// select the lowest ldapId for the current message (see the group-by clause)
// where the recipient type is TO and the message is the same as the main query
subQuery.select(minLdapId)
.where(
cb.and(
cb.equal(sqMessageOwners.get("type"), MessageOwnerType.TO),
cb.equal(sqMessage, correlateMessage)))
.groupBy(sqMessage.get("id"));
// the subquery gives us the lowest TO recipient for each mail
// sort on these values. Note: his must be done here rather than in the Sort property
// of the Pageable in order to maintain the connection between the mailbox in the order by clause
// and the one in the subquery
Path<String> recipientOrderClause = mailbox.get("ldapId");
Order recipientOrder = sortDirection == Sort.Direction.ASC ? cb.asc(recipientOrderClause) : cb.desc(recipientOrderClause);
// secondary sorting descending by sendTime
Order sendTimeOrder = cb.desc(root.get("sendTime"));
// adding order by queries here, rather than via the Sort object
// is something of a violation of standard Spring Data practice. (see above for the reason it is done)
// this precludes us from making some grouped calls such as select count(*)
query.orderBy(recipientOrder, sendTimeOrder);
// attach the subquery onto the query and return.
return cb.equal(mailbox.get("ldapId"), subQuery);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.