JPA:“选择不同的” 带有 BLOB 列

发布于 2024-07-12 04:47:04 字数 315 浏览 9 评论 0原文

我正在尝试运行此 JPQL 查询:

SELECT DISTINCT i FROM Table i JOIN i.other o

它立即失败并显示:

“内部异常:java.sql.SQLException:“BLOB”类型的列不能在 CREATE INDEX、ORDER BY、GROUP BY、UNION、INTERSECT、EXCEPT 或 DISTINCT 语句中使用,因为该类型不支持比较。 ”

这个错误对我来说很有意义,但我该如何解决它呢?

I'm trying to run this JPQL query:

SELECT DISTINCT i FROM Table i JOIN i.other o

which promptly fails with:

"Internal Exception: java.sql.SQLException: Columns of type 'BLOB' may not be used in CREATE INDEX, ORDER BY, GROUP BY, UNION, INTERSECT, EXCEPT or DISTINCT statements because comparisons are not supported for that type."

This error makes sense to me, but how do I get around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

浊酒尽余欢 2024-07-19 04:47:04

您可以将 blob 对象的哈希值或校验和存储在另一列中,并对其使用不同的运算符。

示例:

SELECT i from Table  WHERE id IN (
  SELECT id FROM (
    SELECT MIN(id) AS id, hash_of_i FROM Table GROUP BY hash_of_i
                 ) t
                                )

我确信您可以更优雅地编写此 SQL,但它会给您一个想法。

编辑 - 刚刚意识到使用这个你可以完全省去 Distinct 运算符(它在功能上等同于删除它)。

编辑 2 - 我不确定我的第一个版本是否有效,所以重写了它

You could store a hash or checksum of the blob object in another column, and use your distinct operator on that.

Example:

SELECT i from Table  WHERE id IN (
  SELECT id FROM (
    SELECT MIN(id) AS id, hash_of_i FROM Table GROUP BY hash_of_i
                 ) t
                                )

I'm sure you can write this SQL more elegantly, but it will give you an idea.

Edit - just realised that using this you can dispense with the Distinct operator altogether (it will be functionally equivalent just removing it).

Edit 2 - I am not sure my first version worked, so have rewritten it

十秒萌定你 2024-07-19 04:47:04

HermanD 的一些灵感引导我找到了这个可行的解决方案:

SELECT i FROM Table i WHERE EXISTS (
    SELECT e FROM Table e JOIN e.other o WHERE e.id=i.id
)

Some inspiration from HermanD lead me to this working solution:

SELECT i FROM Table i WHERE EXISTS (
    SELECT e FROM Table e JOIN e.other o WHERE e.id=i.id
)
錯遇了你 2024-07-19 04:47:04

BLOB 类型列中的值只是指向实际数据存储的指针。 为了应用这些运算符中的任何一个,您需要从 BLOB 加载数据并实现您自己的逻辑,因为数据可以表示任何内容(图像、文本...)

Values in columns of type BLOB are only pointers to actual data storage. In order to apply any of these operators you need to load the data from the BLOB and implement your own logic since the data can represent anything (image, text...)

稚然 2024-07-19 04:47:04

在查询中使用 setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) 而不是 unique 。

use setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) instead of distinct on your query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文