gpt4 book ai didi

java - Hibernate @Formula 不支持查询包含 'CAST() as int' 函数

转载 作者:行者123 更新时间:2023-11-29 06:11:35 25 4
gpt4 key购买 nike

下面是ExecutionHistory类的属性之一,取值自@Formula 使用来自 exectution_history 表的 JPA/Hibernate,

@Formula("(SELECT SUM(dividend) || '/' || SUM(divisor) " +
"FROM (SELECT CAST(substr(sr.result, 1, position('/' in sr.result)-1 ) AS int) AS dividend ," +
"CAST(substr(sr.result, position('/' in sr.result)+1 ) AS int) AS divisor " +
"FROM suite_results as sr WHERE sr.execution_history=id) AS division)")
private String result;

当我尝试获取 ExecutionHistory 类的实例时,我发现上面的公式查询由 JPA/Hibernate 像这样转换:

select executionh0_.id as id7_1_, executionh0_.execution_plan as execution3_7_1_, executionh0_.start_time as start2_7_1_, 
(SELECT SUM(sr.duration) FROM suite_results as sr WHERE sr.execution_history=executionh0_.id) as formula0_1_,
(SELECT SUM(executionh0_.dividend) || '/' || SUM(executionh0_.divisor) FROM
(SELECT CAST(substr(sr.result, 1, position('/' in sr.result)-1 ) AS executionh0_.int) AS executionh0_.dividend ,
CAST(substr(sr.result, position('/' in sr.result)+1 ) AS executionh0_.int) AS executionh0_.divisor
FROM suite_results as sr WHERE sr.execution_history=executionh0_.id) AS executionh0_.division) as
formula1_1_, executionp1_.id as id6_0_, executionp1_.build_number as
build2_6_0_, executionp1_.name as name6_0_, executionp1_.owner as owner6_0_, executionp1_.sut as sut6_0_,
executionp1_.wait_for_page_to_load as wait6_6_0_ from execution_history executionh0_
left outer join execution_plans executionp1_ on executionh0_.execution_plan=executionp1_.id where executionh0_.id=?

所以问题是,这里的公式查询包含“CAST() AS int”,但是在 Hibernate 查询转换期间,它放置了不必要的表引用并将其作为“CAST() AS executionh0_.int”执行,所以它给出了 sql执行时出现语法异常。

我不知道如何避免这个问题,有人可以帮助我吗?

谢谢。

最佳答案

这是一个老问题,但无论如何我都会发布一个答案。

如果您使用的是 SQL Server 数据库,则可以在要转换的类型周围添加双引号。像这样:

@Formula("CAST(FLOOR(CAST( dat_criacao AS \"float\")) AS \"datetime\")")

关于java - Hibernate @Formula 不支持查询包含 'CAST() as int' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com