假設有兩個物體:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String firstName;
private String lastName;
@ManyToMany(mappedBy = "authors")
private Set<Book> books;
}
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String fileName;
private String fileType;
@Lob
private byte[] data;
@ManyToMany
@JoinTable(
name = "book_authors",
joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private Set<Author> authors;
}
以下 DTO 介面投影用于僅查詢需要的列。
public interface AuthorView {
String getFirstName();
String getLastName();
Set<BookView> getBooks();
interface BookView {
String getTitle();
}
}
在存盤庫中宣告了一個簡單的findAllBy查詢方法:
public interface AuthorRepository extends JpaRepository<Author, Long> {
@EntityGraph(attributePaths = "books")
List<AuthorView> findAllBy();
}
該方法執行以下查詢:
select
author0_.id as id1_0_0_,
book2_.id as id1_1_1_,
author0_.first_name as first_na2_0_0_,
author0_.last_name as last_nam3_0_0_,
book2_.data as data2_1_1_,
book2_.file_name as file_nam3_1_1_,
book2_.file_type as file_typ4_1_1_,
book2_.title as title4_1_1_,
books1_.author_id as author_i2_2_0__,
books1_.book_id as book_id1_2_0__
from
author author0_
left outer join
book_authors books1_
on author0_.id=books1_.author_id
left outer join
book book2_
on books1_.book_id=book2_.id
即使投影不包含data、file_name和file_type屬性,它們也是從資料庫中獲取的,這會導致性能問題,尤其是在檔案很大的情況下。
根據Thorben Janssen 的博客,問題在于 Spring Data JPA 獲取整個物體并使用它們來執行編程映射。
除了撰寫大量查詢之外,在使用基于介面的 DTO 投影時,是否有任何解決方案可以防止獲取整個物體?
uj5u.com熱心網友回復:
最近,我發現了Blaze Persistence - Entity View Module,看起來很有希望。
根據@Christian Beikov 的回答,EntityView投影幾乎可以像使用Spring Data 集成的Spring Data Projections 一樣使用,并且這些投影僅獲取必要的屬性。此外,不需要使用@EntityGraph,因為集成通過調整查詢生成來臨時處理它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525132.html
