在我的玩家实体中,我尝试使用一个公式来计算所有玩家的得分总和。
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Formula;
import com.google.gwt.user.client.rpc.IsSerializable;
@Entity
public class Player implements IsSerializable {
@Id
private String name;
@Column(nullable = false)
private Integer score;
@Formula(value = " SELECT sum(score) ")
private Integer totalScore = 0;
[...]
}
但是,我在加载播放器时收到语法错误。
Caused by: 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(this_.score) as formula0_0_ from Player this_' at line 1
奇怪的是,这个查询看起来完全有效,当我在与 hibernate 相同的数据库上运行它时,我得到了预期的结果。
SELECT sum(this_.score) as formula0_0_ from Player this_
1 row(s) returned
我已将 hibernate 设置为使用 mysql 方言
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
为什么 jdbc 会为正确的查询抛出错误?
最佳答案
好像是
@Formula(value = " SELECT sum(score) ")
应该是
@Formula(value = " sum(score) ")
相反。有可能(我没试过)发送到 MySQL 的 SQL 查询是
SELECT SELECT sum(this_.score) as formula0_0_ from Player this_
这显然是无效的。
MySQL 仅向您显示遇到错误时的 SQL 字符串。在您的情况下,这没有帮助,因为碰巧从此时起您有一个有效的 SQL 查询。
关于java - 正确查询的 MysqlSyntaxError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703038/