懒加载LAZY和实时加载EAGER的概念,在各种开发语言中都有广泛应用。其目的是实现关联数据的选择性加载,懒加载是在属性被引用时,才生成查询语句,抽取相关联数据。而实时加载则是执行完主查询后,不管是否被引用,立马执行后续的关联数据查询。社区里有人认为懒加载这种功能比较鸡肋,这种事仁者见仁,智者见智啦,个人觉得依自己业务场景而定。

顺带说一句,使用懒加载来调用关联数据,必须要保证主查询session(数据库连接会话)的生命周期没有结束,否则,你是无法抽取到数据的。在Spring Data JPA中如何自己控制session的生命周期

多对一映射

  private Grade grade;
    @JoinColumn(name="grade_id")
    @ManyToOne(fetch=FetchType.LAZY)
    public Grade getGrade() {
        return grade;
    }
@Test
    public void testManyToOne(){

        Grade grade=new Grade();
        grade.setName("ok");

        Student student =new Student();
        Student student1 =new Student();

        student.setName("good");
        student.setGender(1);
        student.setEmail("7879");
        //设置关联关系
        student.setGrade(grade);

        student1.setName("gdd");
        student1.setGender(1);
        student1.setEmail("78de79");
        //设置关联关系
        student1.setGrade(grade);

        entityManager.persist(grade);
        entityManager.persist(student);
        entityManager.persist(student1);

    }

一对多映射

//这时候要去掉joinColumn
@OneToMany(fetch=FetchType.EAGER,mappedBy="grade")
    public Set<Student> getStudent() {
        return student;
    }
    @JoinColumn(name="grade_ids")
    @OneToMany(fetch=FetchType.EAGER)
    public Set<Student> getStudent() {
        return student;
    }
//测试,这时候只有三条insert语句    
    @Test
    public void TestOneToMany(){

        Grade grade=new Grade();
        grade.setName("juewei");

        Student student =new Student();
        Student student1 =new Student();

        student.setName("good1");
        student.setGender(1);
        student.setEmail("7879");


        student1.setName("good2");
        student1.setGender(1);
        student1.setEmail("78de79");


        //设定关系
        grade.getStudent().add(student1);
        grade.getStudent().add(student);

        entityManager.persist(grade);
        entityManager.persist(student);
        entityManager.persist(student1);


    }

双向一对多映射

@Test
    public void TestOneToManyBetween(){

        Grade grade=new Grade();
        grade.setName("juewei");

        Student student =new Student();
        Student student1 =new Student();

        student.setName("good1");
        student.setGender(1);
        student.setEmail("7879");
        student.setGrade(grade);

        student1.setName("good2");
        student1.setGender(1);
        student1.setEmail("78de79");
        student1.setGrade(grade);

        //设定关系
        grade.getStudent().add(student1);
        grade.getStudent().add(student);

        entityManager.persist(grade);
        entityManager.persist(student);
        entityManager.persist(student1);
    }

参考

https://blog.csdn.net/qq_33574890/article/details/78881337
https://blog.csdn.net/u011726984/article/details/72625783
https://www.cnblogs.com/mayuan01/p/12006673.html
https://www.cnblogs.com/MrSi/p/8081811.html
https://blog.csdn.net/aq_112700/article/details/105111419
https://blog.csdn.net/larger5/article/details/79823266#t17
https://www.cnblogs.com/huyuchengus/p/13796107.html
https://blog.csdn.net/dandandeshangni/article/details/79456742