Skip to content
Advertisement

Hibernate many-to-one mapping sets foreign key null

Student has multiple laptops. Student oneToMany Laptop mapping

Student.java

JavaScript

Laptop.java

JavaScript

Main class

JavaScript

After saving the object , foreign key in laptop table is setting as null

lid lName student_id 100 HP NULL

Anyone know where I did wrong mapping due to which I am getting foreign key as null

Thanksin advance

Advertisement

Answer

The “many” side of a 1:many relationship is always the owning side. If the relationship is bidirectional, then the other side will carry a mappedBy attribute, just like the non-owning side of a bidirectional 1:1 relationship. It is the relationship field on the owning side that is meaningful for conveying the relationship to the persistence layer, and you have failed to set that.

For example,

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement