Questions tagged [open-session-in-view]

Open Session In View (OSIV) is pattern where persistence context (session) is open and available in the presentation layer and closed at the end of the HTTP request.

Open Session In View (OSIV) is pattern where persistence context (session) is open and available in the presentation layer and closed at the end of the HTTP request. This pattern became prominent with Spring/Hibernate so it was possible to traverse between ORM (Hibernate/JPA) entities in order to render them without getting the famous "LazyInitializationException" (LIE) from the Hibernate framework.

Pesistence context (session) may be open at the start of the request but there are solutions how to do it on demand - which is preferred if the session is not always necessary.

Pros of the OSIV usage are:

  • it's easy to make changes in the presentation layer without changing the service layer/business logic, if one can traverse to the information across entities (alternative is prefetch everything on the business layer)

Cons of the solution:

  • no clean separation of responsibilities,
  • presentation may fetch objects lazily, lower performance (without service layer to even know about it), often leads to famous N+1 select problem,
  • typically, transaction is closed at the end of the request, which is too late for business logic to treat exceptions (this can be solved using shorter transaction around the business layer call and not around the whole session)
70 questions
125
votes
10 answers

Why is Hibernate Open Session in View considered a bad practice?

And what kind of alternative strategies do you use for avoiding LazyLoadExceptions? I do understand that open session in view has issues with: Layered applications running in different jvm's Transactions are committed only at the end, and most…
HeDinges
  • 4,367
  • 4
  • 30
  • 28
66
votes
1 answer

Trouble using ScrollableResults-backed Stream as return type in Spring MVC

Important note: this has been accepted as a Spring issue with a target fix version of 4.1.2. My goal is to achieve O(1) space complexity when generating an HTTP response from Hibernate's ScrollableResults. I want to keep the standard mechanism…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
13
votes
2 answers

Hibernate Open Session in View: Transaction per Request?

I'm using Hibernate with Spring on Tomcat. I've been reading and re-reading the oft pointed to JBoss wiki page on the topic, and that has been helpful. But it leaves me with some questions. The idea of starting a transaction for every request…
Marvo
  • 17,845
  • 8
  • 50
  • 74
12
votes
1 answer

Configuring OpenSessionInViewFilter with Spring 3 and Servlet 3

i want to configure OpenSessionInViewFilter to able to use hibernate lazy initialization in view, so i added the filter definition in web.xml, but it doesn't work i still get the same lazy initialization exception, here's what i did:
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
4
votes
1 answer

When do we need to .save() an entity in Spring?

In a relatively large Spring Boot project, I have a method with the following (overly simplified) sequence of events: Car car = carRepository.save(new Car()); Person person = personRepository.save(new Person(car)); // Car is a field of…
4
votes
4 answers

Can't get OpenEntityManagerInViewFilter to work in JBoss 6.1

I am trying to add open-session-in-view behavior to an existing pure JPA application. Using Spring in the service-tier is not an option. I would like to wrap the view in Spring's OpenEntityManagerInViewFilter, and not have to modify the EJB…
Alex L.
  • 89
  • 6
3
votes
1 answer

java.lang.ClassNotFoundException: org.springframework.orm.hibernate.support.OpenSessionInViewFilter

i am new to spring/hibernate and was trying out my first app. I got stuck at this error, at first there was something like a 'no session factory bound to thread' error. i resolved it by declaring an OpenSessionInView filter in my web.xml given…
Khizar
  • 2,288
  • 5
  • 32
  • 53
3
votes
2 answers

Spring JSF OpenSessionInViewFilter

i'm having big trouble with getting the OpenSessionInViewFilter working, using Spring 3.0.2, hibernate3 and jsf2. the scenario: there is a BusinessCaseEntity with holding some simple information properties (of string and int type) and a list of…
theFriedC
  • 424
  • 7
  • 20
3
votes
2 answers

LazyInitializationException in spite of OpenSessionInViewFilter

I seem to be randomly getting the following LazyInitializationException in a Spring/MVC 3.0/Hibernate 3.5 application in spite of seeing the filter in the stack trace itself. Any idea on what I should look into? 07 Jun 2011 13:48:47,152 [ERROR] …
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
3
votes
1 answer

hibernate 'open session in view' and asynchronous tasks

I'm utilizing the Open Session in View pattern for my jsf/icesfaces application. As usual a servlet filter is opening and closing all the hibernate sessions at the beginning and the end of a "web server thread". My problem now is that I'm using…
hugri
  • 1,416
  • 3
  • 18
  • 32
3
votes
1 answer

Spring integration tests issue: could not initialize proxy - no Session through reference chain:

I am trying to test a controller method and I have an issue that probably has to do with lazy loading and I am therefore trying to set up a open entity manager in view filter for my tests. Setup method: @Before public void setup() { mockMvc =…
balteo
  • 23,602
  • 63
  • 219
  • 412
3
votes
2 answers

Is it possible to configure OpenSessionInViewFilter in Spring application context so that context:property-placeholder is useable?

Initial situation My web application consists of the Maven modules myapp-persistence(.jar), myapp-model(.jar), myapp-service(.jar) and myapp-web(.war) to get a conventional, loosely coupled, multi-tiered architecture. All modules are joined together…
3
votes
1 answer

Configuring OpenSessionInView filter with Spring Hibernate Application

I am working on a Spring Hibernate web application Earlier I was loading the Spring Configuration with just dispatcher-servlet.xml without using the ContextLoaderListener but when I implement the OpenSessionInView pattern I have to provide a…
underdog
  • 4,447
  • 9
  • 44
  • 89
3
votes
1 answer

Open Session in View vs @Transactional

I had been using @Transactional annotations in my Service Layer. But to resolve an error due to Lazy Loading in View I had to use Open Session in View Filter. After this, without use of @Transaction itself a Session gets opened and transaction…
Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85
3
votes
1 answer

Getting No bean named 'sessionFactory' error when using OpenSessionInViewFilter

I am using Hibernate's lazy loading, and after adding OpenSessionInViewFilter to my web.xml I started to get sessionFactory missing exception, even after defining the sessionFactory bean to use.…
special0ne
  • 6,063
  • 17
  • 67
  • 107
1
2 3 4 5