0

I see with the Spring Boot you don't need to open and close the hibernate session.

But for the understanding purpose how it is working internally, On which layer it is opening the hibernate session and when it is closing.

I created one POC. I have one Spring boot application it has two Entity One is Customer and other is Address and there is one to Many relation between Customer and Address.

And I have one two Apis one is adding record and other is fetching all the records. These APis are there in the CustomerEndpoint and it is annotated with

@RestController
@RequestMapping(value="/customer").

And also created CustomerRepository which extends CrudRepository for saving and fetching the Customer records.

So as per my understanding while fetching the Customer using CustomerRepository inside CustomerEndpointclass should throw LazyInitialization error when we will say customer.getAddress(as its fetchtype is LAZY). But it is not throwing any error, it is working properly.

I was thinking the hibernate session not be there in the CustomerEndpoint class.

Can anyone help me how this Hibernate session is maintained by Spring boot?

As everybody is thinking it as a duplicate of another question but my question is not top of their explanation as according to them session is valid till the Repository so according to that i should get LazyInitialization exception while saying customer.getAddress inside CustomerEndpoint as it is not a Repository but i am not getting any Exception

mksmanjit
  • 244
  • 2
  • 12
  • May be you are looking for this answer: https://stackoverflow.com/questions/25709976/spring-boot-spring-data-how-are-hibernate-sessions-managed – Deepak Khillare May 14 '19 at 09:33
  • Possible duplicate of [Spring Boot & Spring Data: how are Hibernate Sessions managed?](https://stackoverflow.com/questions/25709976/spring-boot-spring-data-how-are-hibernate-sessions-managed) – JEY May 14 '19 at 09:51
  • @DeepakKhillare i already gone through that post but they are unable to answer my question i.e. if the session is at the Repository level then how i am able to fetch the LAZY initialized Address Entity inside CustomerEndpoint, why i did not get LazyInitialization exception – mksmanjit May 14 '19 at 10:18
  • add source code for entities (if annotations has been used). it might have eagerly loaded even though you think that it is lazy loaded. – hunter May 15 '19 at 05:14

2 Answers2

3

First of all it is not a good practice to use Repository layer in your Presentation layer.

OSIV (Open Session in View) is enabled by default in Spring Boot, and OSIV is really a bad idea from a performance and scalability perspective.

Because of this you are not getting the exception and able to work in your presentation layer. Check by putting the follwoings in your application.properties file

spring.jpa.open-in-view=false

You can refer OSIV AntiPattern for more details

DEBENDRA DHINDA
  • 1,163
  • 5
  • 14
  • Thanks, thats correct this is what i am looking for. Correct me if i am wrong, so in spring boot if this flag is false then hibernate session is open only in Repository and wherever you are putting @Transactional, no where else. – mksmanjit May 15 '19 at 05:59
  • which practice?using spring.jpa.open-in-view=true or putting @Transactional? – mksmanjit May 15 '19 at 06:22
  • Yes now i understand, Thanks – mksmanjit May 15 '19 at 06:24
0

I think, It still works If your customer.getAddress is inside the transaction

  • So how can i know whether it is in the **transaction**, because i haven't explicitly added any **Transaction annotation** and as per the **link shared** above they explained that session is valid till the **Repository** but i am in the **CustomerEndpoint** class. So according to that post i should get LazyInitialization exception. – mksmanjit May 15 '19 at 04:58