JPA 映射导致创建错误 table
JPA mapping causes error in create table
我想映射问题和选项之间的关系。
@Entity
public class Question implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long questionId;
private String questionText;
private Integer questionChoices;
private Integer questionNumbers;
@ManyToOne
@JoinColumn(name = "quizId")
private Quiz quiz;
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
private Set<Option> options = new HashSet<Option>();
@OneToOne
@JoinColumn(name = "answerId")
private Answer answer;
// omitting setters and getters hashcode equals methods
}
和
@Entity
public class Option implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long optionId;
private String optionText;
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name = "questionId")
private Question question;
// omitting setters and getters hashcode equals methods
}
但是当我 运行 应用程序时,我将控制台输出为
ERROR: 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 'Option drop foreign key FK_sagu3nkb7af9pwcyqwdp6rggw' at line 1
Hibernate: alter table Question drop foreign key FK_reyly4qc111x1v426cvbyrvld
Hibernate: alter table Question drop foreign key FK_32arg10vsx8ch1qjngdd7mv0d
Hibernate: drop table if exists Option
Mar 03, 2016 7:54:07 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table if exists Option
Hibernate: drop table if exists Question
Mar 03, 2016 7:54:07 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option' at line 1
Hibernate: create table Admin (adminId bigint not null auto_increment, adminEmail varchar(255), adminPassword varchar(255), primary key (adminId))
Hibernate: create table Answer (answerId bigint not null auto_increment, answerCorrect varchar(255), primary key (answerId))
Hibernate: create table History (historyId bigint not null auto_increment, historyCorrect integer, historyDate varchar(255), historyLevel integer, historyScore integer, historyWrong integer, primary key (historyId))
Hibernate: create table Option (optionId bigint not null auto_increment, optionText varchar(255), questionId bigint, primary key (optionId))
Mar 03, 2016 7:54:10 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table Option (optionId bigint not null auto_increment, optionText varchar(255), questionId bigint, primary key (optionId))
Hibernate: create table Question (questionId bigint not null auto_increment, questionChoices integer, questionNumbers integer, questionText varchar(255), answerId bigint, quizId bigint, primary key (questionId))
Mar 03, 2016 7:54:10 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option (optionId bigint not null auto_increment, optionText varchar(255), questi' at line 1
Hibernate: create table Quiz (quizId bigint not null auto_increment, quizCorrectAnswers integer, quizDate varchar(255), quizTag varchar(255), quizTime varchar(255), quizTitle varchar(255), quizTotalQuestions integer, quizWrongAnswers integer, primary key (quizId))
Hibernate: create table Rank (rankId bigint not null auto_increment, time varchar(255), userScore integer, primary key (rankId))
Hibernate: create table User (userId bigint not null auto_increment, userEmail varchar(255), userGender varchar(255), userName varchar(255), userPassword varchar(255), rankId bigint, primary key (userId))
Hibernate: create table histories_quizs (history_id bigint not null, quiz_id bigint not null, primary key (history_id, quiz_id))
Hibernate: create table users_histories (user_id bigint not null, history_id bigint not null, primary key (user_id, history_id))
Hibernate: alter table Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) references Question (questionId)
Mar 03, 2016 7:54:12 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) references Question (questionId)
Mar 03, 2016 7:54:12 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) refe' at line 1
请问映射有误吗
你的映射完全没问题。问题出在实体的名称上:Option
。单词 Option
是 MySQL 中的 reserved/keyword,因此出现了问题。
您可以在日志中看到错误说明:
ERROR: HHH000389: Unsuccessful: create table Option (....
这表明 Option
table 创建失败,因此其他失败如添加外键约束。
给 Option
table 一个不同的 table 名称,例如:
@Entity
@Table(name="ANSWER_OPTIONS")
这应该可以解决您的问题。
我发现 'Group' 也出现了同样的问题。将其重命名为 'Groups' 并且有效。感谢 Mubin 的提示。
我想映射问题和选项之间的关系。
@Entity
public class Question implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long questionId;
private String questionText;
private Integer questionChoices;
private Integer questionNumbers;
@ManyToOne
@JoinColumn(name = "quizId")
private Quiz quiz;
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
private Set<Option> options = new HashSet<Option>();
@OneToOne
@JoinColumn(name = "answerId")
private Answer answer;
// omitting setters and getters hashcode equals methods
}
和
@Entity
public class Option implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long optionId;
private String optionText;
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name = "questionId")
private Question question;
// omitting setters and getters hashcode equals methods
}
但是当我 运行 应用程序时,我将控制台输出为
ERROR: 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 'Option drop foreign key FK_sagu3nkb7af9pwcyqwdp6rggw' at line 1
Hibernate: alter table Question drop foreign key FK_reyly4qc111x1v426cvbyrvld
Hibernate: alter table Question drop foreign key FK_32arg10vsx8ch1qjngdd7mv0d
Hibernate: drop table if exists Option
Mar 03, 2016 7:54:07 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: drop table if exists Option
Hibernate: drop table if exists Question
Mar 03, 2016 7:54:07 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option' at line 1
Hibernate: create table Admin (adminId bigint not null auto_increment, adminEmail varchar(255), adminPassword varchar(255), primary key (adminId))
Hibernate: create table Answer (answerId bigint not null auto_increment, answerCorrect varchar(255), primary key (answerId))
Hibernate: create table History (historyId bigint not null auto_increment, historyCorrect integer, historyDate varchar(255), historyLevel integer, historyScore integer, historyWrong integer, primary key (historyId))
Hibernate: create table Option (optionId bigint not null auto_increment, optionText varchar(255), questionId bigint, primary key (optionId))
Mar 03, 2016 7:54:10 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create table Option (optionId bigint not null auto_increment, optionText varchar(255), questionId bigint, primary key (optionId))
Hibernate: create table Question (questionId bigint not null auto_increment, questionChoices integer, questionNumbers integer, questionText varchar(255), answerId bigint, quizId bigint, primary key (questionId))
Mar 03, 2016 7:54:10 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option (optionId bigint not null auto_increment, optionText varchar(255), questi' at line 1
Hibernate: create table Quiz (quizId bigint not null auto_increment, quizCorrectAnswers integer, quizDate varchar(255), quizTag varchar(255), quizTime varchar(255), quizTitle varchar(255), quizTotalQuestions integer, quizWrongAnswers integer, primary key (quizId))
Hibernate: create table Rank (rankId bigint not null auto_increment, time varchar(255), userScore integer, primary key (rankId))
Hibernate: create table User (userId bigint not null auto_increment, userEmail varchar(255), userGender varchar(255), userName varchar(255), userPassword varchar(255), rankId bigint, primary key (userId))
Hibernate: create table histories_quizs (history_id bigint not null, quiz_id bigint not null, primary key (history_id, quiz_id))
Hibernate: create table users_histories (user_id bigint not null, history_id bigint not null, primary key (user_id, history_id))
Hibernate: alter table Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) references Question (questionId)
Mar 03, 2016 7:54:12 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) references Question (questionId)
Mar 03, 2016 7:54:12 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: 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 'Option add constraint FK_sagu3nkb7af9pwcyqwdp6rggw foreign key (questionId) refe' at line 1
请问映射有误吗
你的映射完全没问题。问题出在实体的名称上:Option
。单词 Option
是 MySQL 中的 reserved/keyword,因此出现了问题。
您可以在日志中看到错误说明:
ERROR: HHH000389: Unsuccessful: create table Option (....
这表明 Option
table 创建失败,因此其他失败如添加外键约束。
给 Option
table 一个不同的 table 名称,例如:
@Entity
@Table(name="ANSWER_OPTIONS")
这应该可以解决您的问题。
我发现 'Group' 也出现了同样的问题。将其重命名为 'Groups' 并且有效。感谢 Mubin 的提示。