简体   繁体   中英

Cannot add or update a child row: a foreign key constraint fails - Hibernate

This is my main 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

@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

@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 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. 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.

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. ( duplication )

You have not wriiten session.save(answer1) Add that and your code will work just fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM