hibernate如何保存三层结构的数据表

各位大神,小弟急求!
我现在想实现以下功能:
学校——班级——学生之间均为一对多的关系,我想实现在保存学校信息的同时能够将班级信息和学生信息均保存。目前代码如下:
学校类:

 @Entity
public class School {
    private String      id;
    private String      name;
    private List<Clazz> clazzSet;

    @OneToMany(mappedBy = "school", fetch = FetchType.LAZY)
    @Cascade(CascadeType.SAVE_UPDATE)
    public List<Clazz> getClazzSet() {
        return clazzSet;
    }

    public void setClazzSet(List<Clazz> clazzSet) {
        this.clazzSet = clazzSet;
    }

    @GeneratedValue(generator = "student")
    @GenericGenerator(name = "student", strategy = "uuid")
    @Id
    @Column(name = "ID")
    public String getId() {
        return id;
    }

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

    @Basic
    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

班级类:

 @Entity
@Table(name = "CLASS", schema = "PUBLIC", catalog = "HIBERNATE")
public class Clazz {
    private String        id;
    private String        num;
    private School        school;
    private List<Student> studentSet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SCHOOL_ID", referencedColumnName = "ID")
    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    @OneToMany(mappedBy = "clazz", fetch = FetchType.LAZY)
    @Cascade(CascadeType.SAVE_UPDATE)
    public List<Student> getStudentSet() {
        return studentSet;
    }

    public void setStudentSet(List<Student> studentSet) {
        this.studentSet = studentSet;
    }

    @GeneratedValue(generator = "student")
    @GenericGenerator(name = "student", strategy = "uuid")
    @Id
    @Column(name = "ID")
    public String getId() {
        return id;
    }

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

    @Basic
    @Column(name = "NUM")
    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

学生类:

 @Entity
public class Student {
    private String id;
    private String name;
    private Clazz clazz;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CLASS_ID", referencedColumnName = "ID")
    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    @GeneratedValue(generator = "student")
    @GenericGenerator(name = "student", strategy = "uuid")
    @Id
    @Column(name = "ID")
    public String getId() {
        return id;
    }

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

    @Basic
    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

测试代码如下:

 @org.junit.Test
    public void testSave() {
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        School school = new School();
        school.setName("北航");
        List<Clazz> clazzList = new ArrayList<>(2);
        Clazz clazz1 = new Clazz();
        clazz1.setSchool(school);
        clazz1.setNum("01");
        Clazz clazz2 = new Clazz();
        clazz2.setSchool(school);
        clazz2.setNum("02");

        List<Student> studentList1 = new ArrayList<>(2);
        Student stu1 = new Student();
        stu1.setClazz(clazz1);
        stu1.setName("zs");
        Student stu2 = new Student();
        stu2.setClazz(clazz1);
        stu2.setName("ls");
        studentList1.add(stu1);
        studentList1.add(stu2);

        List<Student> studentList2 = new ArrayList<>(2);
        Student stu3 = new Student();
        stu3.setClazz(clazz2);
        stu3.setName("ww");
        Student stu4 = new Student();
        stu3.setClazz(clazz2);
        stu4.setName("zl");
        studentList2.add(stu3);
        studentList2.add(stu4);

        clazz1.setStudentSet(studentList1);
        clazz2.setStudentSet(studentList2);
        clazzList.add(clazz1);
        clazzList.add(clazz2);

        school.setClazzSet(clazzList);
        session.save(school);

        transaction.commit();
        session.close();
    }

测试时报错如下:

图片说明

请问各位大神,何解???

2个回答

CLASS_ID不允许为空,检查你的数据库的定义。

id是自动生成的,把fetch = FetchType.LAZY 去掉试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐