不能添加或更新子行:外键约束失败- Hibernate



这是我的主类

public class MappingDemo {
public static void main(String[] args) {

SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

//setting question1 and answer1
Question question1 = new Question();
question1.setQuestionId(1);
question1.setQuestion("What is Java?");

Answer answer1  = new Answer();
answer1.setAnswerId(1);
answer1.setAnswer("Java is programming language.");
question1.setAnswer(answer1);


Session session = factory.openSession();
session.beginTransaction();
session.save(question1);
session.getTransaction().commit();

session.close();
factory.close();
}
}

这是我的问题实体类

@Entity
public class Question {
@Id
@Column(name = "question_id")
private int questionId;
private String question;

@OneToOne
private Answer answer;


public Question() {

}
public Question(int questionId, String question, Answer answer) {
this.questionId = questionId;
this.question = question;
this.answer = answer;
}
public int getQuestionId() {
return questionId;
}
public void setQuestionId(int questionId) {
this.questionId = questionId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public Answer getAnswer() {
return answer;
}
public void setAnswer(Answer answer) {
this.answer = answer;
}
}

答案实体类

@Entity
public class Answer {
@Id
@Column(name = "answer_id")
private int answerId;
private String answer;
public Answer() {
}
public Answer(int answerId, String answer) {
this.answerId = answerId;
this.answer = answer;
}
public int getAnswerId() {
return answerId;
}
public void setAnswerId(int answerId) {
this.answerId = answerId;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}

和我的配置XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
<property name="connection.username">root</property>
<property name="connection.password">parkash92#</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping class="com.kash.hibernateproject.entities.Answer"/>
<mapping class="com.kash.hibernateproject.entities.Question"/>

</session-factory>
</hibernate-configuration>

,我在控制台上得到这个,我使用Hibernate,你可以在xml配置文件中看到。根据教程,我遵循这应该在数据库中添加两行,每个表中一个,并将它们与外键(answer_id)链接,但它给了我这个错误。

Oct 25, 2021 9:45:16 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Cannot add or update a child row: a foreign key constraint fails (`hibernatedb`.`question`, CONSTRAINT `FKs6ghcwuovtcp489oo5dy7rvk5` FOREIGN KEY (`answer_answer_id`) REFERENCES `answer` (`answer_id`))

您没有写session.save(answer1)添加它,你的代码就可以正常工作了

最新更新