Announcement Announcement Module
No announcement yet.
OpenSessionInViewFilter and Still getting a Lazy Initialization Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter and Still getting a Lazy Initialization

    Hi there I have got on my web.xml the OSIVF, see

    After that I have got a genericDao with encapsulated criterias like:

    public final List<?> getAll(Class<?> EntityType, Boolean LazyInitialize, Boolean FullLazyInitialize, Integer sessionNumber) {
            Session session = SessionFactoryUtils.getSession(getSessionFactory(), Boolean.FALSE);
            Criteria criteria = session.createCriteria(EntityType);
            List<Object> list = criteria.list();
            return list;
    In my code I call sometime codes like this

    usuariosInterface.setUsuarios((List<Usuario>) genericDao.getAll(Usuario.class));
    This list will populate a datatable and when I click a button on this datatable open details from the Usuario entity.

    But I got an LazyInitializationException when I click It.

    So I tried just to to an HibernateInitialize

    But I am still getting a LazyInitializationException.

    Id like to know where should I initialize it, I guess the session should be opened right?

    Thanks a lot

  • #2
    I suggest a read on OpenSessionInView... The session is open UNTIL the view is rendered, so if you keep a list of objects in memory those objects will still be detached and thus need to be reloaded, unless you initialize them before view rendering (in your controller for instance).


    • #3
      Thanks a lot

      So, how would you suggest-me to do?

      I dont want to initilizate each component on my list because it would be very expensive for SQL and memory.

      Do you suggest-me to open a new session and call a getById on usuario again initilizint its collections?

      Thank you


      • #4
        I think that you are not using the filter for the correct scenario. In case you need to load data on button click open/get hibernate session & use it.
        The filter is useful for scenarios where due to transaction boundary on service layer, the hibernate session is not availaible to web layer. This filter ensures that session is availaible to web layer to initlialize the laziliy loaded collections.