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
3
votes
1 answer

GWT RequestFactory with Set sub-collections

I have a little problem with RequestFactory regarding persistence of children collections in the shape of Set . I am using gwt 2.5 with requestfactory, and Hibernate4/Spring3 at the backend. I am using the open-session-in-view filter by Spring so…
2
votes
1 answer

Should Rollbacks be an exception? (specifically: in web-apps and with OSIV)

This is a rather theoretical question: In my service classes I tend to always do a rollback if the operation cannot take place for whatever reason, even if the cause is false input or any other condition that's expected and checked. In a…
Lunikon
  • 3,751
  • 4
  • 23
  • 20
2
votes
2 answers

"failed to lazily initialize a collection of role" exception occurs even when I use OSIV pattern

I'm using OpenSessionInViewInterceptor to avoid LazyInitializationException. It worked fine, but from sometime LazyInitializationException occurs even I'm using OSIV. The exceptions are like this: SEVERE: Servlet.service() for servlet [appServlet]…
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
2
votes
2 answers

JPA @Entity not managed after creation?

I have a simple controller method, where I create a new object Car and then set its name to Audi: @GetMapping(value = "/resource") public ResponseEntity visit() { Car car = carRepo.save(new Car("VolksWagen")); // Car should be managed now? …
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
2
votes
1 answer

Single transaction per request with Spring MVC and Hibernate

I want to use Spring MVC and Hibernate. I don't want to use service layer or @Transactional attribute on all controller methods (or, rather, I want Spring to treat them all as transactional). So I want to start transaction when controller method…
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
2
votes
1 answer

Hibernate Session scope in JTA transactions vs Open-Session-In-View

Is it correct to say that using JTA Transactions with Hibernate contrasts using the Open-Session-In-View with regards to the session scope? From what I've been able to gather the Session scope in the JTA Transactions is a transaction (mainly based…
Ittai
  • 5,625
  • 14
  • 60
  • 97
2
votes
1 answer

Hibernate Criteria list doesn't return new records immediate

I'm using Spring MVC with Hibernate 4. My problem is when I use Criteria with list() method to fetch all records from the table. There is a 3rd party process which inserts records to the table every 1 second, and a screen which represent this table…
Tal Etinger
  • 113
  • 1
  • 1
  • 8
2
votes
0 answers

How to keep OpenSessionInView only use stateless session by default on Spring.NET/NHibernate?

our project only query data over a legacy database, can we use a stateless session by default when spring framework auto injected session by OSIV situation? I mean the base class NHibernateRepository's method GetCurrentSession can retrieve a…
Steven Wang
  • 145
  • 9
2
votes
1 answer

sessionFactory.getCurrentSession() returns different sessions although OSIV is configured

I have issue with Spring's OpenSessionInViewFilter. My web.xml config looks like that: contextConfigLocation WEB-INF/applicationContext.xml …
eugen-fried
  • 2,111
  • 3
  • 27
  • 48
2
votes
5 answers

How to make OpenSessionInViewFilter exclude static resources

I have implemented the OpenSessionInViewFilter for my MVC webapp and it works almost perfect. Only problem is that it also creates a session for every image, js, css etc that are requested from the webserver. This i dont want. Im using struts2,…
user829237
  • 1,719
  • 8
  • 37
  • 61
1
vote
1 answer

Spring.NET + NHibernate + request scoped objects not playing well together

I'm using Spring.NET 1.3.2, NHibernate 3.1 and the OSIV pattern in a ASP.NET application. I have a custom EventListener that needs to be request scoped because it uses HttpContext.Current.Items as a constructor dependency. Since ISession is also…
1
vote
1 answer

OpenSessionInViewFilter doesn't work

Please somebody rescure me, I cannot lazy load my class, I tried to write Open Session In View strategy, but it seems not to work. web.xml:
Cichy
  • 1,319
  • 3
  • 20
  • 36
1
vote
2 answers

Is Session.close() has effect on Spring openSessionInView?

I have openSessionInView filter in web.xml. openSessionInView org.springframework.orm.hibernate3.support.OpenSessionInViewFilter And I have set allowCreate property…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
1
vote
0 answers

Open session in view with annotation

Is it possible to use an annotation to do whatever OpenSessionInView filter does: to keep hibernate session open in scope but for only one annotated function? PS I would need this for local fixes in some legacy code.
MS13
  • 327
  • 5
  • 16
1
vote
2 answers

Open Session in View pattern using Hibernate, Spring, Struts 2

I'm working on a project using Hibernate for persisting and Struts 2 for the view pattern. My configuration files are: web.xml: //...... //.....
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37