简体   繁体   English

Hibernate 多对一映射集外键 null

[英]Hibernate many-to-one mapping sets foreign key null

Student has multiple laptops.学生有多台笔记本电脑。 Student oneToMany Laptop mapping学生 oneToMany 笔记本电脑映射

Student.java学生.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笔记本电脑.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主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("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();
    }

}

After saving the object, foreign key in laptop table is setting as null保存 object 后,笔记本电脑表中的外键设置为 null

lid lName student_id 100 HP NULL盖子 lName student_id 100 HP NULL

Anyone know where I did wrong mapping due to which I am getting foreign key as null任何人都知道我在哪里做了错误的映射,因为我得到外键为 null

Thanksin advance提前致谢

The "many" side of a 1:many relationship is always the owning side. 1:many 关系的“多”方始终是拥有方。 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.如果关系是双向的,那么对方将携带一个mappedBy属性,就像双向 1:1 关系的非拥有方一样。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM