JPA映射会在create table中导致错误

时间:2016-03-03 15:11:58

标签: java jpa spring-data-jpa

我想映射问题和选项之间的关系。

@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

映射中是否有任何错误。

2 个答案:

答案 0 :(得分:3)

你的映射完全没问题。问题在于实体的名称:Option。单词Option是MySQL中的保留/关键字,因此存在问题。

您可以在日志中看到错误声明: ERROR: HHH000389: Unsuccessful: create table Option (....

这表明Option表创建失败,因此其他失败如添加外键约束。 为Option表提供不同的表名,例如:

@Entity @Table(name="ANSWER_OPTIONS")

这应该可以解决你的问题。

答案 1 :(得分:0)

我发现&#39; Group&#39;出现了同样的问题。将其重命名为&#39; Groups&#39;它的工作原理。谢谢你的提示Mubin。