Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Does anybody use Jackson for JSON with Spring 3.0 ContentNegotiating?

  1. #1
    Join Date
    Jan 2010
    Posts
    10

    Unhappy Does anybody use Jackson for JSON with Spring 3.0 ContentNegotiating?

    I'm using jackson to generate the JSON response.

    Architecture:
    1.Spring 3.0, configured with ContentNegotiatingViewResolver to server XML and JSON response.
    2.Spring MVC, annotation. Most entities have one and more relationship with another entities. For example : parent one-to-many child.
    3.Web page, I'm plan to use a existed JQuery grid plugin to present data. The grid can support pagenation, cell editor. It use JSON the data source.

    Problem:
    It works well when generating html response. It also can generate XML. But when generating JSON, will throw a exception :
    Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.aaa.bbb.domain.User.roles, no session or session was closed
    looks like a problem caused by jackson. When jackson try to fetch associated objects, there is no session opened.

    I searched the internet, found some solutions. But they are not suitable to my case because they are managing the session themself. In my case, I'm using Spring to manage the session.

    What can I do? Or is there something wrong with my solution?
    Thanks in advance!

    Update:
    for this situation , I can solve it through set the FetchType.Eager. But for those entities which have many associated entities, if i can set all to FetchType.Eager. I think there must arise a performance problem. What can I do?
    Last edited by recoco.zhang; Feb 23rd, 2010 at 08:20 PM. Reason: update

  2. #2
    Join Date
    Jan 2010
    Posts
    10

    Default

    any suggestion?

  3. #3

    Unhappy JsonMappingException: failed to lazily initialize a collection of role

    Hey,

    I have exactly the same problem as you describe...

    Did you already find a solution ? I'm a bit desperate...

    thnx!

  4. #4
    Join Date
    Jan 2010
    Posts
    10

    Default

    I still can't find any solution.Look like I also should subscribe a post in Jackson own forum.

  5. #5
    Join Date
    Aug 2008
    Posts
    15

    Default

    Hi,

    use DTO objects instead of using hibernate entities directly.

  6. #6

    Default DTO objects

    Hey Boban,

    Thnx for responding, but I already make use of DTO objects. My DAO's make use of the HibernateDaoSupport class so I can use hibernateTemplate to make the calls... In my beans I convert the retrieved Domain objects into TO objects before I hand them over to my controller...

    Like Recoco said, this works for XML view resolvers and HTML view resolvers, but it doesn't for JSON view resolvers... I think it's a problem of JSON Jackson Mapper

  7. #7
    Join Date
    Jun 2007
    Location
    Northern VA
    Posts
    15

    Default

    Weird it shouldn't work for any. What is happening is Jackson is calling a getter on your POJO for a collection that hasn't yet been initialized by Hibernate. The call generates a call to the database but you get the exception because your hibernate session is closed. Currently using HibernateTemplate is discouraged and using the Hibernate Session is the recommended approach. Also you should be using the OpenSessionInView Servlet Filter so that your Hibernate Session is open for the duration of the request.

  8. #8

    Default opensessioninview

    Thanks for replying novahokie.. I've tried using opensessioninview servlet filter as well as opensessioninview interceptor,, again...it works for XML but not for JSON...maybe you're right and I should try managing my own session instead of using hibernate template, but still...like you said...it should work...

  9. #9
    Join Date
    Jun 2007
    Location
    Northern VA
    Posts
    15

    Default

    Yeah I understand that but the underlying problem has nothing to do with Jackson or the JSON support. A lazy loading exception is 100% caused by bad management of the Hibernate Session. To see what I mean on your ManyToOne or ManyToMany annotation for User.roles put fetch=FetchType.EAGER which basically turns off lazy loading for that collection and then it should work but that's just a bandaid.

    The correct solution is to use the OpenSessionInView filter or interceptor and in your DAO classes call sessionFactory.getCurrentSession() which will return the current open Hibernate Session object. Then with that session you can call session.load(User.class, id) to get the User object. Unless you let spring manage the session in this way you will always end up dealing with lazy init problems.

    http://blog.springsource.com/2007/06...r-jpatemplate/

  10. #10
    Join Date
    Jan 2010
    Posts
    10

    Default

    Thank you very much, novahokie. I'll try opensessioninview.

Tags for this Thread

Posting Permissions

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