Results 1 to 9 of 9

Thread: ZK Ajax Framework Integration with Spring and Hibernate

  1. #1

    Default 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. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3

    Default

    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

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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.

  6. #6

    Default

    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

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote 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

  8. #8
    Join Date
    Aug 2005
    Posts
    3

    Default

    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

  9. #9

    Default

    Quote 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •