JPQL 与字符串函数不同
我有一个像这样的 JPQL:
select distinct d
from Department d
left join fetch d.employees
当我想要获取我的 Department 实体的惰性属性之一时,distinct 不再起作用。
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 中进行转换,从而限制了数据库和应用程序之间传输的数据量。网络服务器。
作为解决方法,我尝试将查询分为两部分:
select d, substring(d.htmlDescription, 1,400)
from Department d where d in (
select distinct d1
from Department d1 left join fetch d1.employees
)
这不起作用,因为 JOIN FETCH
不得在子查询的 FROM
子句中使用。
I have a JPQL like this one:
select distinct d
from Department d
left join fetch d.employees
When I want to fetch one of the lazy property of my Department entity, the distinct is not working any more.
select distinct d, substring(d.htmlDescription, 1,400)
from Department d
left join fetch d.employees
The query returns as much Department as the number of employees in it.
The substring(d.htmlDescription)
is important because the property is defined as a CLOB (type TEXT under postgresql):
@Column(columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
String htmlBody;
The substring function is translated in sql thus limiting the amount of data transfered beetween the database and the web server.
As a workaround, I tried to break the query in two parts :
select d, substring(d.htmlDescription, 1,400)
from Department d where d in (
select distinct d1
from Department d1 left join fetch d1.employees
)
This doestn't work because the JOIN FETCH
must not be used in the FROM
clause of a subquery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,我找到了解决问题的方法:
htmlBody 字段现在位于另一个实体中。这样部门实体就更轻了。
然后我可以使用以下请求:
这样,我就有 2 个 sql 查询。员工的获取是在第二个查询中完成的,该查询发生在少量数据上。子串是用SQL实现的。完美的!
Finally I found a solution to my problem by :
The htmlBody field is now in another entity. Thus the departement entity is lighter.
I can then use the following requests :
That way, I have 2 sql queries. The fetching of employees is done in the second query witch occurs on a small amount of datas. The substring is realized in SQL. Perfect!
由于我无法发表评论,我想指出一些我认为可疑的事情。
distinct d, substring(d.htmlDescription, 1,400)
返回的对象是什么?您可以使用单独的查询获取该字符串,或者使用 Java 获取该字符串吗?Since I cannot make comments, I would like to point out few things that stick out to me as doubtfull.
distinct d, substring(d.htmlDescription, 1,400)
? Could you fetch that String with separate query, or get that substing using Java?