Results 1 to 3 of 3

Thread: Detaching an object from a session in Spring?

  1. #1
    Join Date
    Mar 2005
    Posts
    19

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

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

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    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
    Alef Arendsen
    SpringSource
    http://www.springsource.com

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Replies: 0
    Last Post: Aug 25th, 2005, 05:11 AM
  3. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  4. Replies: 5
    Last Post: Jun 10th, 2005, 03:21 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

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