1

What I want to do is "get all schools with its students" but It doesn't initialize student list. Some people says change it to FetchType.EAGER but I have no choice like updating it and also I don't think its a solution. Is there a way to initialize it?

@Service
public class SchoolService {
    @Autowired
    private SchoolRepository schoolRepository;

        public List<School> getAllSchools() {
            return schoolRepository.findAll();
        }
}


public class School {

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "school_student", joinColumns = {@JoinColumn(name = "school_id")},
    inverseJoinColumns = {@JoinColumn(name = "student_id")})
    private List<Student> students= new ArrayList<>();
    ...
}

EDIT: I am using spring data jpa 1.10.1

hellzone
  • 5,393
  • 25
  • 82
  • 148
  • If you change it to FetchType.EAGER, is it fetching the results ? – Vasu Oct 31 '16 at 13:23
  • Are you seeing any error ? Try running it inside the transaction, user @Transactional. – Vikram Palakurthi Oct 31 '16 at 13:26
  • which JPA version do you use ? – davidxxx Oct 31 '16 at 13:27
  • 1
    answers here give some options: http://stackoverflow.com/questions/15359306/how-to-load-lazy-fetched-items-from-hibernate-jpa-in-my-controller - either defining your own @Query method that eager loads, rather than using `findAll`. Or messing around with the `@EntityGraph` annotation on your repository interface. Either way, I don't think you can use `findAll`: you'll have to have your own custom method – David Lavender Oct 31 '16 at 13:28
  • 1
    @Mr Spoon I agree with but entitygraph require jpa 2.1 – davidxxx Oct 31 '16 at 13:49

0 Answers0