需要將帳戶串列轉換為 accountDto。問題是如何?
@Query("SELECT ac.account FROM Academy ac WHERE ac.id = :uuid")
fun findAcademyAccountsById(uuid: UUID, pageable: Pageable): List<AccountDto>
uj5u.com熱心網友回復:
取決于您在尋找什么樣的性能Account和外觀。AccountDto您可以手動或使用映射器(例如 MapStruct)將物體物件轉換為 Java 代碼中的 DTO 物件。
我認為這是Blaze-Persistence Entity Views的完美用例。
我創建了該庫以允許在 JPA 模型和自定義介面或抽象類定義模型之間輕松映射,例如 Spring Data Projections on steroids。這個想法是您以您喜歡的方式定義您的目標結構(域模型),并通過 JPQL 運算式將屬性(getter)映射到物體模型。
使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:
@EntityView(Account.class)
public interface AccountDto {
@IdMapping
Long getId();
String getName();
}
查詢是將物體視圖應用于查詢的問題,最簡單的就是通過 id 進行查詢。
AccountDto a = entityViewManager.find(entityManager, AccountDto.class, id);
Spring Data 集成允許您幾乎像 Spring Data Projections 一樣使用它:https ://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
Page<AccountDto> findAll(Pageable pageable);
最好的部分是,它只會獲取實際需要的狀態!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/513451.html
上一篇:無法加入兩個表JPA
下一篇:來自同一子查詢的最小值和最大值
