Home > Software engineering >  Hibernate many-to-one mapping sets foreign key null
Hibernate many-to-one mapping sets foreign key null

Time:11-27

Student has multiple laptops. Student oneToMany Laptop mapping

Student.java


import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Student {
    
    @Id 
    private int id;
    private StudentName studentName;
    private String email;
    
    @OneToMany(mappedBy = "student")
    private List<Laptop> laptops = new ArrayList<Laptop>();
    
    public Student() {
    }


    public Student(int id, StudentName studentName, String email) {
        this.id = id;
        this.studentName = studentName;
        this.email = email;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public StudentName getStudentName() {
        return studentName;
    }


    public void setStudentName(StudentName studentName) {
        this.studentName = studentName;
    }


    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }

    public List<Laptop> getLaptop() {
        return laptops;
    }


    public void setLaptop(List<Laptop> laptops) {
        this.laptops = laptops;
    }


    @Override
    public String toString() {
        return "Student [id="   id   ", studentName="   studentName   ", email="   email   "]";
    }   
}

Laptop.java

package com.practice.hibernateDemo.enity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Laptop {

    @Id
    private int lid;
    private String lName;
    
    @ManyToOne
    @JoinColumn(name="student_id", referencedColumnName="id")
    private Student student;
    
    public Laptop() {
    }

    
    public int getLid() {
        return lid;
    }

    public void setLid(int lid) {
        this.lid = lid;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "Laptop [id="   lid   ", lName="   lName   "]";
    }
}

Main class

package com.practice.hibernateDemo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.practice.hibernateDemo.enity.Laptop;
import com.practice.hibernateDemo.enity.Student;
import com.practice.hibernateDemo.enity.StudentName;

public class CreateStudent {

    public static void main(String[] args) {

        Laptop laptop = new Laptop();
        laptop.setLid(100);
        laptop.setlName("HP");      
        
        Student student = new Student();
        
        student.setId(101);
        student.setEmail("[email protected]");
        student.setStudentName(new StudentName("test1","test2", "test3"));
        student.getLaptop().add(laptop);
        
        Configuration con = new Configuration().configure().addAnnotatedClass(Student.class).addAnnotatedClass(Laptop.class);

        SessionFactory sf = con.buildSessionFactory();

        Session session = sf.getCurrentSession();

        Transaction tx = session.beginTransaction();
        
        session.save(laptop);
        session.save(student);
        
        tx.commit();
    }

}

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

CodePudding user response:

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,

laptop.setStudent(student);
  • Related