Announcement Announcement Module
Collapse
No announcement yet.
ZK Ajax Framework Integration with Spring and Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ZK Ajax Framework Integration with Spring and Hibernate

    I tried in my web.xml the code bellow and in same case it works very well, but sometimes it throw the popular "LazyInitializationException"

    Sometimes, the following lines: Set<SubMenu> subMenus = employee.getSubMenu(); throws the popular "LazyInitializationException"

    Did spring take care about the new world of ajax framework? Or the filter is made for only traditional web app?

    In web.xml
    <!-- Hibernate OpenSession Filter -->
    <filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class> org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter
    </filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value>
    </init-param>
    <init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value>
    </init-param>
    </filter>

    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- Fim filter -->

    Mapping In Employee.java:
    @ManyToMany
    @JoinTable(name = "Employee_SubMenu", joinColumns = { @JoinColumn(name = "employee") }, inverseJoinColumns = { @JoinColumn(name = "submenu") })
    @OrderBy("id")
    private Set<SubMenu> submenus = new HashSet<SubMenu>();

    Any Comments or help I will appreciate.

    Thanks in advance,

    Marcos de Sousa

  • #2
    hello

    i never work before with OpenSessionInViewFilter and Annotations

    but i will try to help
    Sometimes, the following lines: Set<SubMenu> subMenus = employee.getSubMenu(); throws the popular "LazyInitializationException"
    ok, the trick is that each element of the Set need an Initialization

    getHibernateTemplate().initialize(yourobject);//one
    now if each object has a reference to other class and you need to see this object in the form, it must has an Initialization too, so below of "one"
    getHibernateTemplate().initialize(yourobject.getOt herObject());

    how do you return the Set collection, thats means, call to some DAO object to be filled??

    HTH
    regards

    Comment


    • #3
      The Implementation:

      public Employee getByUserName(String username) {
      return (Employee) getHibernateTemplate().find(
      "FROM Employee WHERE username = ?", username).get(0);
      }

      I will try your idea, and read more about getHibernateTemplate().initialize method.

      But I will repete "Sometimes, the following lines: Set<SubMenu> subMenus = employee.getSubMenu(); throws the popular "LazyInitializationException""

      So, it looks like is effect of Ajax Framework.

      Thanks for your help.

      Regards,

      Marcos de Sousa

      Comment


      • #4
        hello

        Code:
        public Employee getByUserName(String username) {
        return (Employee) getHibernateTemplate().find(
        "FROM Employee WHERE username = ?", username).get(0);
        }
        try in this way
        Code:
        public Employee getByUserName(String username) {
        Employee employee = (Employee) getHibernateTemplate().find(
        "FROM Employee WHERE username = ?", username).get(0);
        getHibernateTemplate().initialize(employee);
        return employee;
        }
        But I will repete "Sometimes, the following lines: Set<SubMenu> subMenus = employee.getSubMenu(); throws the popular "LazyInitializationException""
        yes, you right, sometimes, and i cant explain the reason of that
        but i know how avoid the "LazyInitializationException"

        So, it looks like is effect of Ajax Framework.
        i dont think so, i used to work with ajax, but tell me how you use ajax with the Set variable?

        regards

        Comment


        • #5
          I would guess you get the exception as the root entity is loaded in one Session and then you try to lazy load the assocication in another Session. OSIV is only going to work for you in context of one request.

          Comment


          • #6
            but i know how avoid the "LazyInitializationException"
            To avoid is simple I can make it like @ManyToMany(fetch = FetchType.EAGER) take it eagered, I think it is what getHibernateTemplate().initialize(employee); do

            Maybe the answer I will live with is from
            I would guess you get the exception as the root entity is loaded in one Session and then you try to lazy load the assocication in another Session. OSIV is only going to work for you in context of one request.
            But, your answer was very helpfull, and I would like to thanks very help.

            Regards,

            Marcos de Sousa

            Comment


            • #7
              Originally posted by sousa1981 View Post
              To avoid is simple I can make it like @ManyToMany(fetch = FetchType.EAGER) take it eagered, I think it is what getHibernateTemplate().initialize(employee); do
              Eagerly initializing it will ensure that everything is loaded "upfront" so yes this a solution to the problem.
              http://www.hibernate.org/hib_docs/re...rformance.html

              Comment


              • #8
                I posted a solution to a similiar / same problem here, if you're still having problems:

                http://coderoony.blogspot.com/2007/0...th-spring.html

                Comment


                • #9
                  Originally posted by paran0rmal View Post
                  I posted a solution to a similiar / same problem here, if you're still having problems:

                  http://coderoony.blogspot.com/2007/0...th-spring.html
                  Thanks, it had been solved.
                  But, the link was very helpfully.


                  Regards,

                  Marcos de Sousa

                  Comment

                  Working...
                  X