I have two objects Mat and MatImage, parent and child resp. The primary key of MatImage is the id of Mat and they are join by one to one relationship.
If I understand bidirectional relation correctly, the child object would know about the parent if I do something like matImage.setMat(mat). I would think the primary key would be filled at this point, but it doesn't. I know this because sql throws an exception when it try to insert MatImage with #0 as matId.
Another question is with the n+1 problem. I would like to lazily load child object because not all mat has matimage. I can try changing @OneToOne to @ManyToOne but not sure how that can be done bidirectionally. Any help would be appreciated. Thank you.
Thank you very much for your reply. I corrected the mapping by putting @PrimaryKeyColumn in owned entity and Removed @JoinColumn when mapped by is used. I guess I am still confused with the benefit of using PrimaryKeyJoinColumn annotation since I still have to set matId in MatImage. I was hoping to do something like
And expects the matId in matImage to be populated because matId is id in Mat object. Am I missing something? Thank you.