java - Hibernate,当我使 id 列自动递增时,sql 出现错误

标签 java mysql spring hibernate

我在 Spring 中使用 Hibernate 5 进行 ORM。

我的 table 是:

public class Match {

    @Id
    @Column(name="match_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private ArrayList<String> players;
    //@OneToMany(mappedBy="match")
    @OneToMany(mappedBy="match")
    private List<Game> games;
    ...
} 

public class Game {

    @Id
    @Column(name="game_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String person1;
    private String person2;
    private String winner;
    private int score1;
    private int score2;
    private int game_number;

    @Column(name="match_id")
    @ManyToOne(fetch=FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private Match match;
}

两个表之间的关系是一场比赛包含多场比赛。

但是我收到自动增加的错误,错误是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match (match_id integer not null auto_increment, players tinyblob, primary key (' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

由于出现错误和中断,我不知道如何打印sql。 不知道只是设置id自动增加有错误吗? 我用manytoone的家伙wikibooks ,我的情况与EMPLOYEE(表)和PHONE(表)情况类似。

最佳答案

Match 是 MySql 中的保留关键字,这就是导致错误的原因。将您的实体重命名为不同的名称,例如 FootballMatch..

关于java - Hibernate,当我使 id 列自动递增时,sql 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728507/

相关文章:

java.awt.HeadlessException - 小程序未显示

Java 代码没有捕获错误? (初学者)

c# - 我可以在 Mysql 的一个连接字符串下创建多个不同的插入查询吗?

PHPExcel 从定义的工作表生成数据到数组

java - 为什么以下模式验证不起作用

java - 如何从 Spring Condition 访问文件属性?

java - RxJava 流 : conditional operators and error handling

java - 如何将速度表达式转换为 JSP?

php - 仅显示数据库中的选定值

java - 我正在恢复一个数据库,我需要在我的 Java 应用程序中锁定它的任何 Activity