我在Spring Data JPA中遇到了一些簡單查詢的性能問題。Spring Data JPA - 使用ElementCollection進行查詢 - 性能低下
型號:
@Entity
public class LogEntry {
..
@NotNull
@ElementCollection(fetch = FetchType.EAGER)
private List<String> parameters;
@ManyToOne
private Guest guest;
..
}
庫:
Page<LogEntry> findByGuestOrderByCreationDateDesc(Guest guest, Pageable pageable);
我想顯示的所有參數的日誌條目列表。但查詢速度非常慢。找到所有條目後,它開始查詢每個條目的參數。
日誌顯示大量的這些行:
Hibernate: select parameters0_.LogEntry_id as LogEntry1_8_0_, parameters0_.parameters as paramete2_9_0_ from LogEntry_parameters parameters0_ where parameters0_.LogEntry_id=?
我正在尋找,以提高查詢的方式。我嘗試使用聯合抓取而沒有成功。
@Query("select l from LogEntry l join fetch l.parameters where l.guest = ?1 order by l.creationDate desc")
Caused by: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,collection join,fetch join,fetch non-lazy properties,classAlias=null,role=domain.guest.LogEntry.parameters,tableName={none},tableAlias=parameters1_,origin=null,columns={,className=null}}] [select count(l) from domain.guest.LogEntry l join fetch l.parameters where l.guest = ?1]
問題不在於您提供的查詢。這是Spring從查詢中派生出來的count查詢。 AFAIR,你可以在Query註釋中指定一個countQuery(它不會對連接獲取(這應該是一個左連接fetc,BTW))。 http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/Query.html#countQuery-- –