Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewFilter and Still getting a Lazy Initialization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    Code:
    <filter>
            <filter-name>hibernateFilter</filter-name>
            <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
            <init-param>
                <param-name>sessionFactoryBeanName</param-name>
                <param-value>sessionFactory</param-value>
            </init-param>
        </filter>
        
        <filter-mapping>
            <filter-name>hibernateFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    After that I have got a genericDao with encapsulated criterias like:

    Code:
    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

    Code:
    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

    Code:
     Hibernate.initialize(usuariosController.getUsuario().getPerfis());
    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).

    Comment


    • #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

      Comment


      • #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.

        Comment

        Working...
        X