Announcement Announcement Module
No announcement yet.
Problem with usage of various hibernate sessions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with usage of various hibernate sessions


    I have some issues with hibernate and would like some clarifications.
    The problem relates to the usage of various hibernate sessions.

    My application is a Web Application over struts / spring / hibernate and the problem is that I load a pojo from hibernate and obtain a connected pojo, then I disconnect it and keep it in the Web session for some business logic through various request's from the client.
    When I try to save the changed pojo I obtain an exception that says that the pojo was already connected to an old session.
    when the save method is called I dont have the initial hibernate session because spring gave me another one.
    The problem is that when I disconnected the pojo it really wasn't disconnected. What I used to disconnect the pojo was the session method evict.
    Tracking the execution I realized that the session removes this entry from the inner collecions, the problem is that somehow the pojo never forgets the link with the original session.

    I have done some experiments and when I have a proxy to a pojo, I can connect it and disconnect it just fine because I
    have access to his lazyInitializer. The problem is that I can't get the proxy for the initial pojo so I can't detach sucessfully the pojo so it could be saved by another Hibernate session.

    What do you suggest I do:
    - another way to detach the pojos ( which??) or try to get the original session and use it ( problem is
    when it get's closed)
    - Try to use always the same hibernate session by Spring transaction.

    Honestly I was hopping you could give me some info of how to manage detaching pojos between Hibernate sessions in an easy manner.

    Another thing is that sometimes I get proxys for the pojos and sometimes I get the real pojo, is there a way to get only proxys in my querys?

    Best Regards
    Afonso Remédios

  • #2
    You are trying to find the wrong solution to the problem - read the Hibernate reference documentation or HB in Action for several patterns on this problem. Instead of trying to keep the old session you can reattach your existing detached object to a new session.
    Hibernate offers plenty of support for this and actually recommends it.