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