java - @Formula - Hibernate 中的 native 查询语法错误

标签 java mysql spring hibernate jpa

我在 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/

相关文章:

java - C# 中反序列化 json java 对象

javascript - Spring Boot 通过嵌入式 Tomcat 在 web-application.jar 之外提供外部 HTML/javascript/css 服务?

mysql - 查询 MYSQL 特定时间最近 7 天的数据

PHP Mysql 字段值基于另一个字段值

mysql 查询到 querybuilder

java - 带有 JPA 存储库的 Spring Boot 静态 where 子句

Java Spring 4 验证失败时,它不渲染表单,而是简单地打印 View 名称

java - 集群环境下如何管理spring cache

java - 如何在 JVM 级别捕获 OutOfMemory 错误?

java - 在icefaces中选择BooleanCheckbox