在我的玩家实体中,我试图使用一个公式来包含所有玩家的得分总和。
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
我已将休眠设置为使用 mysql 方言
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
为什么 jdbc 会为正确的查询抛出错误?