Spring data jpa fetch lazy

I have confused regarding below behavior in spring data jpa

@Table(name = "T_STUDENT")
public class Student {

@OneToMany(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
	@JoinColumn(name = "student_id")
	private List<Address> addresses;


even though it’s lazy when I tried to fetch address its load address object. My question why i am not getting LazyInitializeExceptiion. When i see logs it’s call db to test address object.

Thank you

Maybe because of the default Open Session in View used by Spring Boot, which is a bad idea anyway.

so if i set


it will give lazyinitializationexception. right ?

After you get out the @Transactional context and the EntityManager is closed, yes. Inside the transnational block, no.

but i didn’t define it in @Transaction block. See below code

public void createPrivateMethod(StudentDTO studentDTO) {
		Student student = new Student();
		Address address = new Address();
		List<Address> addressList = new ArrayList<>();
		address = new Address();
		address.setCity(studentDTO.getAddress() + "1");
		List<Student> findAll = this.studentRepository.findAll();
		findAll.stream().forEach(s -> s.getAddress().forEach(Address::getCity));

Add a breakpoint and check the stacktrace for the TransactionInterceptor. If you see it, you are in a transactional block.

ok will check and inform you tommrrow.

Thanks vlad

Actually, since this is a Spring issue, you should address it on the Spring forum. Thanks.

Yes it should be. Thanks vlad