我在 FullProduct 和 PartValue 之间存在一对多关系(一个 Full Product 有多个 PartValue)
我正在尝试使用下面的 @Formula 注释 - 语法似乎是正确的,因为我在命令行中运行它并给出了正确的结果。
@Formula("select sum(pv.total_value) from part_value pv inner join full_product fp on fp.full_product_id=pv.full_product_id")
private Float totalAmount;
但是,当我在 Spring JPA/Hibernate 中运行它时,出现以下错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select sum(pv.total_value) from part_value pv inner join full_product fp on fp.full_product_id=pv.full_product_id' at line 1
错误日志中没有其他内容,似乎指向了正确的方向。
我很困惑——如果命令行中的 SQL 语法运行良好,为什么 Spring/Hibernate 会抛出这个错误。有人可以帮忙吗?
感谢您的指点。
最佳答案
尝试为子查询添加(
and )
。
@Formula("(select sum(pv.total_value) from part_value pv inner join full_product fp on fp.full_product_id=pv.full_product_id)")
private Float totalAmount;
因为它被翻译成了类似的东西
select
entityAlias1.field1,
entityAlias1.field2,
...
(select sum(pv.total_value) from part_value pv inner join full_product fp on fp.full_product_id=pv.full_product_id),
...
from the_entity_table entityAlias1
...
没有 () 语法是错误的。
关于java - @Formula - Hibernate 中的 native 查询语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44591289/