[英]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.