FetchType.LAZY not working for @manyToMany in Spring jpa data

I have 3 objects: University, Student and Subject. University and Student have many, many relationships, and the student and subject also have many, many relationships. They are executed as shown below. University:

 @ManyToMany(fetch=FetchType.LAZY)
        @JoinTable(
            name="UNIV_TO_STD_REL"
            , joinColumns={
                @JoinColumn(name="UNIV_DBKY", referencedColumnName="UNIV_DBKY")
                }
            , inverseJoinColumns={
                @JoinColumn(name="STD_DBKY", referencedColumnName="STD_DBKY")
                }
            )
        private List<Student> students;<br>

Student:

@ManyToMany(mappedBy="students")
    private List<University> universities;
@ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(
        name="STD_TO_SUB_REL"
        , joinColumns={
            @JoinColumn(name="STD_DBKY", referencedColumnName="STD_DBKY")
            }
        , inverseJoinColumns={
            @JoinColumn(name="SUB_DBKY", referencedColumnName="SUB_DBKY")
            }
        )
    private List<Subject> subjects;<br>

Topic:

@ManyToMany(mappedBy="subjects")
    private List<Student> students;

Now I want to get students for a specific university. But not related topics. So, I invoke the repository as shown below:

University u=UniversityRepo.findByUnivId("1234");
u.getStudents(); 

But this returns to me a list of students along with related topics from the Subject. It seems that when I call u.getStudents (), he not only selects the students, but also receives the items, although FetchType is LAZY.
Please suggest.

EDIT: UniversityRepo

public interface UniversityRepo extends JpaRepository<University, BigInteger> {

    public University findByUnivId(String id);
}
+5
1

, SpringEboity LocalEntityManagerFactory, spring-orm. FactoryBean , JPA . LocalContainerEntityManagerFactoryBean, / , , Java EE EntityManagerFactory JNDI. 2 : Spring JPA, LocalContainerEntityManagerFactoryBean

0

Source: https://habr.com/ru/post/1607453/


All Articles