JPA one to one mapping

I have the following table structure with a mapping from one to one.

public class Parent { private long parentPK(Parent primary key); private Child child; @OneToOne( cascade = {CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "child" ) public Child getChild() { return this.child; } } public class Child{ private long childPK(Child primary key); private Parent parentFK;(parent foreign key) @OneToOne @JoinColumn( name = "PARENT_FK", referencedColumnName = "PARENT_PK" ) public Parent getParentFK() { return this.parentFK; } } 

Note. The parent table does not have a reference to the child table. The child contains the parent primary key as a foreign key column. Here the primary key is different for the parent and child.

According to my requirement, when saving the parent record, the child record should be saved automatically without explicitly specifying the foreign key for the child table. Executing with the above structure results in "Cannot insert non-zero entry for PARENT_FK in table CHILD." Can anyone kindly advise on the possibility of achieving the above requirement?

+4
source share
3 answers

The display is incorrect.

The main problem you have here is that mappedBy = "child" refers to the child attribute of the Child class, not the Parent class. There is no child attribute in the child class, and you need to refer to the parentPK attribute in the Child class instead.

I suggest leaving PK / FK out of the entity field names, as this can be a bit confusing.

Below are the updated Parent / Child children. (Ive switched to field access, but that's just my preference)

 @Entity public class Child { @Id @Column(name = "childPK") private Long id; @OneToOne @JoinColumn( name = "PARENT_FK", referencedColumnName = "PARENT_PK" ) private Parent parent; @Entity public class Parent { @Id @Column(name = "PARENT_PK") private long id; @OneToOne( cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, --> mappedBy = "parent") //Refers to parent field of the Child class private Child child; 

Note that you have defined the OneToOne bidirectional relationship. JPA bidirectional relationships have their own side β€” the side with the connection information, Child in your case β€” and the flip side is the side with the mappedBy attribute, the parent in your case. It is a good idea to handle this. This link reviews OneToOne relationships, and there are many others around. https://en.wikibooks.org/wiki/Java_Persistence/OneToOne

+1
source

Your mapping is correct, and you did not specify a save code for the save operation.

But you cannot save the child without reference to the parent, you will not need to set the foreign key, but in your save method you must do the following:

 parent.setChild(child); child.setParent(parent); 
+1
source

I came across something similar. Your display and everything looks good. It looks like you installed the parent of the Child object and did not set the Parent for this child object. That is, you are trying to save a child without setting its parent. You may need to do the following:

 Parent p= new Parent(); Child c = new Child(); p.setChild(c); // c.setParent(p); Looks like this is missing in your code session.save(); 
0
source

All Articles