1 回答

TA貢獻1798條經驗 獲得超7個贊
是的,我是這么認為的。我評論說“我認為你的查詢沒問題,但是當結果被編組為 JSON 時,所有相關的部門都會被檢索。你應該在編組之前查看你的 sql 輸出并調試和檢查查詢結果,看看是否是案子。” 我繼續玩弄它,我或多或少是正確的。
問題是您沒有custDept
使用查詢獲取集合,因此當客戶為您的其余響應編組時,將執行附加查詢以獲取值,而附加查詢只查詢所有內容。
2019-05-25 14:29:35.566 DEBUG 63900 --- [nio-8080-exec-2] org.hibernate.SQL:選擇不同的 customer0_.customer_no 作為 customer1_0_,customer0_.customer_name 作為 customer2_0_,customer0_.industry 作為 industry3_0_ 來自客戶 customer0_ left outer join customer_department custdept1_ on customer0_.customer_no=custdept1_.customer_no where custdept1_.dept_name like ? 限制 ?
2019-05-25 14:29:35.653 DEBUG 63900 --- [nio-8080-exec-2] org.hibernate.SQL:選擇 custdept0_.customer_no 作為 customer4_1_0_,custdept0_.dept_id 作為 dept_id1_1_0_,custdept0_.dept_id 作為 dept_1_1_1_1_1 .customer_no 作為 customer4_1_1_,custdept0_.dept_name 作為 dept_nam2_1_1_,custdept0_.primary_contact 作為 primary_3_1_1_ 來自 customer_department custdept0_ where custdept0_.customer_no=?
如果您只想要查詢提供的內容,則需要進行提取,以便在custDept
編組之前初始化集合。您的查詢還存在其他問題。你應該使用一個 sql 參數:deptName
并且你應該聲明它,你應該提供一個countQuery
因為你要返回一個Page
.
public interface CustomerRepository extends JpaRepository<Customer,Integer>{
@Query(value="select DISTINCT c from Customer c left join fetch c.custDept cd where cd.deptName like %:deptName% ",
countQuery = "select count ( DISTINCT c ) from Customer c left join c.custDept cd where cd.deptName like %:deptName% ")
public Page<Customer> findByName(@Param("deptName") String deptName, Pageable pageable);
為我工作?,F在只執行了原始查詢,結果是正確的。
{
"content": [
{
"customerNo": 1,
"custDept": [
{
"deptName": "it"
}
]
}
],
最后請注意,最好根據 spring 文檔在您的實體中使用Integerfor 。@Id
添加回答
舉報