我有這樣一個JPQL:JPQL不同與字符串函數
select distinct d
from Department d
left join fetch d.employees
當我想去找我Department實體的懶財產之一,明顯是行不通的任何更。
select distinct d, substring(d.htmlDescription, 1,400)
from Department d
left join fetch d.employees
該查詢返回的部門數量與其中的員工數量一樣多。
的substring(d.htmlDescription)
是重要的,因爲該屬性被定義爲CLOB(下PostgreSQL類型TEXT):
@Column(columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
String htmlBody;
substring函數被翻譯在SQL從而限制了數據的轉移beetween數據庫和web量服務器。
作爲一種變通方法,我試圖打破查詢兩個部分:
select d, substring(d.htmlDescription, 1,400)
from Department d where d in (
select distinct d1
from Department d1 left join fetch d1.employees
)
這doestn't的工作,因爲JOIN FETCH
不能子查詢的FROM
子句中使用。
1.我添加了使用子字符串jpql函數而不是java的原因。 – GaetanZ
2.左連接用於提取。這是我想要在該查詢上加載的唯一懶惰關係。沒有獲取結果是N + 1選擇。 – GaetanZ
3.我不明白你的建議。 – GaetanZ