Announcement Announcement Module
Collapse
No announcement yet.
Detaching an object from a session in Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detaching an object from a session in Spring?

    I'm using OpenSessionInViewInterceptor and all the other Hibernate/Spring goodies. I'm using HibernateSynch with the Spring templates as well.

    How do you detach an object and reassociate it with another object, in the case that Spring manages all of your sessions for you and you don't have access to the session object?

    Here's what I'm trying to do:
    1) I get a request http://joe.com?member=1
    2) This gets dispatched into a SimpleFormController
    3) I use referenceData to load up a member object with id=1, to check if that member exists
    4) Then in the view, if the member exists, then I automatically load that information into the text fields, if not, I make everything blank

    I'm okay so far...

    5) However, when the user hits submit, then in onSubmit() I get passed a command object with the same id as in step 3
    6) When I save the new command object with all the new fields, this results in an error:

    Code:
    org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 5, of class: org.phatcast.db.Member; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 5, of class: org.phatcast.db.Member
    So it's clear to me that the reason is that in Step 3, the member object is getting attached to the session, and then when I try and save another object in 5, it breaks.

    I know how to handle this in Hibernate without Spring, since I can just close sessions or otherwise detach the objects.

    In the case where Spring is managing my sessions and I don't have direct access to the session object, how can I detach the object in Step 3 and reassociate it with the new object in Step 5?

    Many thanks on this.

    Andrew
    Seattle, WA

  • #2
    I don't have direct access to the session object
    Take a look at this post

    Comment


    • #3
      You'd have to lookup the Session using SessionFactoryUtils.getSession(SessionFactory). You need the sessionfactory for this, which you'll have to lookup manually in you application context.

      Hope this helps.

      rgds,
      Alef Arendsen

      Comment

      Working...
      X