有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate,当我对id列进行自动递增时,sql出现错误

我在春天用Hibernate5做ORM

我的桌子是:

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 fellowwikibooks,我的情况类似于EMPLOYEE(table)和PHONE(table)案例


共 (1) 个答案

  1. # 1 楼答案

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