Siswa memiliki beberapa laptop. Pemetaan Laptop oneToMany Siswa

Mahasiswa.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 + "]";
    }
}

Kelas utama

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("test@gmail.com");
        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();
    }

}

Setelah menyimpan objek, kunci asing di tabel laptop disetel sebagai null

Tutup lNama student_id 100 HP NULL

Adakah yang tahu di mana saya melakukan pemetaan yang salah karena saya mendapatkan kunci asing sebagai nol

Terima kasih sebelumnya

0
Rushabh Chalikwar 27 November 2021, 07:15

1 menjawab

Jawaban Terbaik

Sisi "banyak" dari hubungan 1: banyak selalu menjadi sisi pemilik. Jika relasinya dua arah, maka sisi lain akan membawa atribut mappedBy, seperti sisi non-pemilik dari relasi 1:1 dua arah. Ini adalah bidang hubungan di sisi pemilik yang bermakna untuk menyampaikan hubungan ke lapisan persistensi, dan Anda telah gagal mengaturnya.

Sebagai contoh,

laptop.setStudent(student);
0
John Bollinger 27 November 2021, 07:33