[英]Cannot add or update a child row: a foreign key constraint fails - Hibernate
This is my main class这是我的主要 class
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();
}
}
This is my Question Entity class这是我的问题实体 class
@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;
}
}
Answer Entity class回答实体class
@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;
}
}
and my configuration xml file和我的配置 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>
and I am getting this on console, I am using Hibernate as you can see in the xml config file.我在控制台上得到了这个,我正在使用 Hibernate,正如你在 xml 配置文件中看到的那样。 According to the tutorial I am following this should add two row in database, one in each table and link them with foreign key (
answer_id
) but it gives me this error.根据我正在遵循的教程,这应该在数据库中添加两行,在每个表中添加一行并将它们与外键(
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`))
if it can help you, when using Heidi databse editor tool, I get the same error trying to create foreign key ( error ) that is a key duplication.如果它可以帮助你,当使用 Heidi 数据库编辑器工具时,我在尝试创建外键(错误)时遇到同样的错误,这是一个键重复。 ( duplication )
(重复)
You have not wriiten session.save(answer1) Add that and your code will work just fine你还没有写 session.save(answer1) 添加它,你的代码就可以正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.