【问题标题】:join two tables in oracle db with hibernate - one table has clob data使用休眠连接 oracle db 中的两个表 - 一个表具有 clob 数据
【发布时间】:2019-05-10 17:58:00
【问题描述】:

所以我已经为此苦苦挣扎了大约 8 个小时,这是我整个移动应用程序中的最后一块。请帮我将此伪代码转换为有效的 HQL。我在底部的内存数据库中有一个在 H2 中工作的语句,但我的产品数据库是 oracle,它非常挑剔。我正在尝试获取排行榜并添加 base64 图像。即使我删除了下面的不同的工作 H2 语句,它仍然不会运行并出现错误`

不一致的数据类型:预期 - 得到 CLOB

.

声明我无法在 HQL 中工作:

select a.*, b.image 
from (select distinct score.staffId as staffId, 
      sum(score.totalScore) as totalScore, 
      sum(score.timeTaken) as timeTaken from Score score) a 
join User b 
on a.staffId=b.staffId"

H2 中的工作声明:

"select distinct score.staffId as staffId, 
        sum(score.totalScore) as totalScore, 
        sum(score.timeTaken) as timeTaken, 
        user.image as image"
+ " from Score score, User user"
+ " where score.staffId = user.staffId"
+ " group by score.staffId order by totalScore desc, timeTaken asc"

【问题讨论】:

  • 你的表结构是什么?哪些列是 LOB?
  • 您的 Oracle 查询完全错误,您的 H2 查询也是如此(尽管它正在工作)。两者都遭受与 MySQL 相关的不当行为,尽管编写错误,聚合却奇迹般地工作。

标签: java oracle hibernate hql


【解决方案1】:

尽管我对HQL一无所知,但我会尝试从不同的角度回答您的问题。

将您的查询重写为...

select a.*, b.image
from (
        select staffId,
            sum(totalScore) as totalScore,
            sum(timeTaken) as timeTaken
        from Score
        group by staffId
    ) a
    join User b
        on a.staffId = b.staffId

...它应该可以工作。

如果仍然没有,那么您刚刚见证了称为“复杂视图合并”的 Oracle 查询优化器功能的奇迹,您将不得不通过 no_merge 提示稍微帮助您的 Oracle 数据库...

select a.*, b.image
from (
        select --+ no_merge
            staffId,
            sum(totalScore) as totalScore,
            sum(timeTaken) as timeTaken
        from Score
        group by staffId
    ) a
    join User b
        on a.staffId = b.staffId

至于您的 ORM,不要将其用于 DB 读取,而应使用纯 SQL 或 jOOQ。 ORMs are perfect at writing stuff to DB, not at reading it.

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2015-12-04
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    相关资源
    最近更新 更多