JPQL 与字符串函数不同

发布于 2024-12-06 03:24:18 字数 898 浏览 0 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-12-13 03:24:18

最后,我找到了解决问题的方法:

  1. 修改我的映射
  2. ,将请求分为两次调用。

htmlBody 字段现在位于另一个实体中。这样部门实体就更轻了。

class Department{
...
@OneToOne (fetch = FetchType.LAZY, 
    cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
Content content = new Content();
...
}


class Content{
...
@Column(columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
String htmlBody;
...
}

然后我可以使用以下请求:

    List<Department> deps = em.get().createQuery(
            "select distinct d " +
                    "from Department d " +
                    "order by d.id desc ", Department.class)
            .setFirstResult(first)
            .setMaxResults(count)
            .getResultList();

    List<Object[]> tuple = em.get().createQuery(
            "select d, substring(d.content.htmlBody, 1,400)" +
                    "from Department d " +
                    "left join fetch d.employees" +
                    "where d in (:deps) order by d.id desc")
            .setParameter("deps", deps)
            .getResultList();

    ... //Filter the duplicates due to the fetching

这样,我就有 2 个 sql 查询。员工的获取是在第二个查询中完成的,该查询发生在少量数据上。子串是用SQL实现的。完美的!

Finally I found a solution to my problem by :

  1. modifying my mapping
  2. cutting the request in 2 calls.

The htmlBody field is now in another entity. Thus the departement entity is lighter.

class Department{
...
@OneToOne (fetch = FetchType.LAZY, 
    cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
Content content = new Content();
...
}


class Content{
...
@Column(columnDefinition = "TEXT")
@Basic(fetch = FetchType.LAZY)
String htmlBody;
...
}

I can then use the following requests :

    List<Department> deps = em.get().createQuery(
            "select distinct d " +
                    "from Department d " +
                    "order by d.id desc ", Department.class)
            .setFirstResult(first)
            .setMaxResults(count)
            .getResultList();

    List<Object[]> tuple = em.get().createQuery(
            "select d, substring(d.content.htmlBody, 1,400)" +
                    "from Department d " +
                    "left join fetch d.employees" +
                    "where d in (:deps) order by d.id desc")
            .setParameter("deps", deps)
            .getResultList();

    ... //Filter the duplicates due to the fetching

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!

埋情葬爱 2024-12-13 03:24:18

由于我无法发表评论,我想指出一些我认为可疑的事情。

  1. distinct d, substring(d.htmlDescription, 1,400) 返回的对象是什么?您可以使用单独的查询获取该字符串,或者使用 Java 获取该字符串吗?
  2. 我相信该查询可以重写为没有左连接语句的查询。
  3. 也许您可以重写查询,以便可以首先放置子字符串语句,然后放置不同的 d?

Since I cannot make comments, I would like to point out few things that stick out to me as doubtfull.

  1. What is the object returned with distinct d, substring(d.htmlDescription, 1,400)? Could you fetch that String with separate query, or get that substing using Java?
  2. I would trust that that query can be rewritten into one without left join statement.
  3. Maybe you could rewrite the query so you could put substring statement first and then distinct d?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文