Announcement Announcement Module
Collapse
No announcement yet.
problem updating a composite object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem updating a composite object

    hi,

    i have the following situation where i am trying to do a update on a data object which holds another data object as a field. for example a Item object has a price object.

    When I try to do the update I get the following eexception in my logs:

    caught data access exception org.springframework.orm.hibernate3.HibernateSystem Exception: a different object with the same identifier value was already associated with the session: [com.transferobjects.HajjVO#1]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.transferobjects.HajjVO#1]

    My Hajj object has a client object associaited with it with the following mapping:
    Code:
    <many-to-one name="client" column="client_id" 
    	    	class="com.transferobjects.ClientVO"/>
    The code throws the exception on the following. It loads the client object fine:
    Code:
    ClientVO client = clientDAO.findClient&#40;clientId&#41;;
    hajj.setClient&#40;client&#41;;			
    
    //save 
    hajjDAO.saveHajjDetail&#40;hajj&#41;;
    Can anyone explain to me why it sayes i have another object associated.

    It seems I have two hajj objects floating around in my session but cant understand why

  • #2
    just some more information:

    I call my delegate where i get the error from a struts action class.

    The action class basically maps my form in to the value object then calls the delegate.

    The delegate uses a DAO to write the data which has code as follows:

    Code:
    log.debug&#40;"entered the saveHajjDetail"&#41;;
    		HibernateTemplate hibTemp = getHibernateTemplate&#40;&#41;;
    		
    		hibTemp.saveOrUpdate&#40;hajj&#41;;
    		
    		log.debug&#40;"exiting the saveHajjDetail"&#41;;
    I cant think that I have two of the same objects in thes ession.

    Comment


    • #3
      The object your are trying to save has been already associated with the session - this is why HB gives you this message. See the official HB FAQ - I think they answer this question in detail.

      Comment


      • #4
        I had a look at the FAQ and could not find a decent answer. If what you say is right how do I disassociate one of the objects from the session.

        Is it possible that when I do the search and issue a select call it associates the object with the session the first time? But when i do my update how can I do this without creating a new Hajj object to add the details of the form into. I set the resourceId to be the same as the object i retrieved from the DB.

        Comment


        • #5
          The solution depends pretty much on the logic flow of the code. One very quick hack in HB3 is to use session.merge() instead of saveOrUpdate().
          If you are stick with HB2 then I suggest you turn logging on and see what objects are present inside the session. Note that if you have an object with id 3 you can't associate with the session another object with the same id because HB will not know which one to use. You have to either update the existing object or clear the session first cache or save the new object with a new session.

          Comment


          • #6
            i tried clearing the session with the flush() but that did not work.

            The merge() method enables me to update but is this the best way to do it or is it just a hack andnot something that should be used in the long term

            Comment


            • #7
              You should read the hibernate documentation. Flush() doesn't clear the session but rather persists the changes (flushes the changes to the db). Look at clear().

              merge() has other semantics on how objects are saved and associated with the session. Read the reference documentation and javadocs.

              Comment


              • #8
                I have gone for the option of clearing the session before i perform the update and that seems to work.

                What would be the consequences of doing it this way? I dont have anything in the cache before I clear it which i want to write to the DB.

                Below is extract from log. It seesm it fell over because of the detached object but still am not sure how it came about

                Code:
                15&#58;41&#58;33,780 DEBUG AbstractBatcher&#58;299 - about to close ResultSet &#40;open ResultSets&#58; 1, globally&#58; 1&#41;
                15&#58;41&#58;33,780 DEBUG AbstractBatcher&#58;284 - about to close PreparedStatement &#40;open PreparedStatements&#58; 1, globally&#58; 1&#41;
                15&#58;41&#58;33,780 DEBUG AbstractBatcher&#58;392 - closing statement
                15&#58;41&#58;33,790 DEBUG Loader&#58;490 - total objects hydrated&#58; 1
                15&#58;41&#58;33,790 DEBUG TwoPhaseLoad&#58;96 - resolving associations for &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;41&#58;33,790 DEBUG DefaultLoadEventListener&#58;193 - loading entity&#58; &#91;com.medina.transferobjects.ClientVO#9&#93;
                15&#58;41&#58;33,790 DEBUG DefaultLoadEventListener&#58;240 - creating new proxy for entity
                15&#58;41&#58;33,790 DEBUG TwoPhaseLoad&#58;167 - done materializing entity &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;41&#58;33,790 DEBUG PersistenceContext&#58;738 - initializing non-lazy collections
                15&#58;41&#58;33,790 DEBUG TransactionSynchronizationManager&#58;129 - Retrieved value &#91;org.springframework.orm.hibernate3.SessionHolder@1290ef4&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@14a97b&#93; bound to thread &#91;http-8080-Processor24&#93;
                15&#58;41&#58;33,790 DEBUG HajjDAOHibernate&#58;57 - exiting findExistingHajj
                15&#58;41&#58;33,790 DEBUG HajjDAOHibernate&#58;57 - exiting findExistingHajj
                15&#58;41&#58;33,790 DEBUG HajjDAOHibernate&#58;38 - entered the saveHajjDetail
                15&#58;41&#58;33,790 DEBUG HajjDAOHibernate&#58;38 - entered the saveHajjDetail
                15&#58;43&#58;03,160 DEBUG TransactionSynchronizationManager&#58;129 - Retrieved value &#91;org.springframework.orm.hibernate3.SessionHolder@1290ef4&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@14a97b&#93; bound to thread &#91;http-8080-Processor24&#93;
                15&#58;43&#58;03,160 DEBUG TransactionSynchronizationManager&#58;129 - Retrieved value &#91;org.springframework.orm.hibernate3.SessionHolder@1290ef4&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@14a97b&#93; bound to thread &#91;http-8080-Processor24&#93;
                15&#58;43&#58;03,170 DEBUG Cascades&#58;525 - id unsaved-value&#58; null
                15&#58;43&#58;03,170 DEBUG AbstractSaveEventListener&#58;412 - detached instance of&#58; com.medina.transferobjects.HajjVO
                15&#58;43&#58;03,180 DEBUG DefaultSaveOrUpdateEventListener&#58;200 - updating detached instance
                15&#58;43&#58;03,190 DEBUG DefaultSaveOrUpdateEventListener&#58;246 - updating &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;43&#58;03,190 DEBUG TransactionSynchronizationManager&#58;129 - Retrieved value &#91;org.springframework.orm.hibernate3.SessionHolder@1290ef4&#93; for key &#91;org.hibernate.impl.SessionFactoryImpl@14a97b&#93; bound to thread &#91;http-8080-Processor24&#93;
                15&#58;43&#58;49,998 ERROR HajjDelegateImpl&#58;84 - caught data access exception org.springframework.orm.hibernate3.HibernateSystemException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;; nested exception is org.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;43&#58;49,998 ERROR HajjDelegateImpl&#58;84 - caught data access exception org.springframework.orm.hibernate3.HibernateSystemException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;; nested exception is org.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;43&#58;50,008 DEBUG RuleBasedTransactionAttribute&#58;119 - Applying rules to determine whether transaction should rollback on com.medina.DataUnavailableException&#58; Data Access Exception org.springframework.orm.hibernate3.HibernateSystemException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;; nested exception is org.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;
                15&#58;43&#58;50,008 DEBUG RuleBasedTransactionAttribute&#58;137 - Winning rollback rule is&#58; null
                15&#58;43&#58;50,008 DEBUG RuleBasedTransactionAttribute&#58;143 - No relevant rollback rule found&#58; applying superclass default
                15&#58;43&#58;50,008 DEBUG TransactionInterceptor&#58;292 - com.medina.business.HajjDelegate.saveHajj threw throwable &#91;com.medina.DataUnavailableException&#58; Data Access Exception org.springframework.orm.hibernate3.HibernateSystemException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;; nested exception is org.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; &#91;com.medina.transferobjects.HajjVO#3&#93;&#93; but this does not force transaction rollback
                15&#58;43&#58;50,008 DEBUG HibernateTransactionManager&#58;527 - Triggering beforeCommit synchronization
                15&#58;43&#58;50,008 DEBUG HibernateTransactionManager&#58;542 - Triggering beforeCompletion synchronization
                15&#58;43&#58;50,008 DEBUG HibernateTransactionManager&#58;400 - Initiating transaction commit
                15&#58;43&#58;50,018 DEBUG HibernateTransactionManager&#58;484 - Committing Hibernate transaction on session &#91;org.hibernate.impl.SessionImpl&#40;PersistentContext&#91;entitiesByKey=&#123;EntityKey&#91;com.medina.transferobjects.HajjVO#3&#93;=com.medina.transferobjects.HajjVO@1f00&#125;&#93; ActionQueue&#91;insertions=&#91;&#93; updates=&#91;&#93; deletions=&#91;&#93; collectionCreations=&#91;&#93; collectionRemovals=&#91;&#93; collectionUpdates=&#91;&#93;&#93;&#41;&#93;
                15&#58;43&#58;50,018 DEBUG JDBCTransaction&#58;83 - commit
                15&#58;43&#58;50,018 DEBUG SessionImpl&#58;292 - automatically flushing session
                15&#58;43&#58;50,018 DEBUG AbstractFlushingEventListener&#58;52 - flushing session
                15&#58;43&#58;50,018 DEBUG AbstractFlushingEventListener&#58;102 - processing flush-time cascades

                Comment


                • #9
                  What would be the consequences of doing it this way?
                  Not much. Possibly a small performance hit depending on your usage. This is due to possibly having to reload objects into the session.

                  I dont have anything in the cache before I clear it which i want to write to the DB.
                  Hmmm...you must have had at least that one object in the session, otherwise Hibernate wouldn't have complained.

                  It seesm it fell over because of the detached object but still am not sure how it came about
                  Costin summed it up nicely:
                  Note that if you have an object with id 3 you can't associate with the session another object with the same id because HB will not know which one to use.

                  Comment

                  Working...
                  X