简体   繁体   English

Hibernate:java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败

[英]Hibernate: java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I'm trying to create one to one relationship in Jpa.我正在尝试在 Jpa 中创建一对一的关系。 When I run this program throw the Exception当我运行这个程序时抛出异常

ERROR: Cannot add or update a child row: a foreign key constraint fails (`ngsharma`.`student`, CONSTRAINT `FK8nqh8nm4hrwx9hlqwhxf6kfen` FOREIGN KEY (`laptop_lid`) REFERENCES `laptop` (`lid`))

Student学生

@Entity(name = "student")
public class Student {

    @Id 
    private  int rollno;
    private String name;    
    @OneToOne 
    private Laptop laptop;

    /*Setter & Getter*/
}

Laptop笔记本电脑

@Entity(name = "laptop")
public class Laptop {

    @Id
    private int lid;
    private String laptopName;

    /*Setter & Getter*/
}

Hibernate.cfg.xml Hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ngsharma</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MariaDBDialect</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

Run CLass运行 CLass

    Configuration configuration = 
            new Configuration().configure()
                                                    .addAnnotatedClass(Student.class).addAnnotatedClass(Laptop.class);

    SessionFactory sessionFactory = configuration.buildSessionFactory();

    Session session = sessionFactory.openSession();

    Laptop laptop = new Laptop();
    laptop.setLid(101);
    laptop.setLaptopName("Mac Nootbook");

    Student student = new Student();
    student.setRollno(1);
    student.setName("Shri Krishan");
    student.setLaptop(laptop);

     Serializable serializable = session.save(student);
     System.out.println("Test Code : "  + serializable);

     session.beginTransaction().commit();

In your example, you are trying to save a student entity, for which laptop association does not exist in database.在您的示例中,您正在尝试保存一个student实体,该实体的laptop关联在数据库中不存在。 Both instances are newly created as far as understand from the code snippet you provided.据您提供的代码片段了解,这两个实例都是新创建的。 Hence, according to the foreign constraint at database level, you are trying to reference laptop table from within student table;因此,根据数据库级别的外部约束,您正在尝试从student表中引用laptop表; where the corresponding laptop row with the referenced id does not exist.其中不存在具有引用id的相应laptop行。

If you also persist the associated laptop entity while persisting the student entity your problem will be solved.如果您在保留student实体的同时还保留关联的laptop实体,您的问题将得到解决。 The only modification you need to do is to update the @OneToOne relation as:您需要做的唯一修改是将@OneToOne关系更新为:

    @OneToOne(cascade=CascadeType.PERSIST)  
    private Laptop laptop;

This will work;这将起作用; since it will also insert the laptop entity to the database along with the student entity itself.因为它还将laptop实体与student实体本身一起插入数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - getting java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails Spring Boot - java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - Spring Boot - java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails Hibernate:无法添加或更新子行:外键约束失败 - Hibernate :Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束失败 - Hibernate Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败Hibernate - Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束在休眠状态下失败 - Cannot add or update a child row: a foreign key constraint fails in hibernate 休眠:无法添加或更新子行:外键约束失败 - Hibernate:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Hibernate - Cannot add or update a child row: a foreign key constraint fails - Hibernate SpringBoot App 抛出 ava.sql.SQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败 - SpringBoot App throws ava.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM