Announcement Announcement Module
Collapse
No announcement yet.
AOP transaction doesnīt flush Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP transaction doesnīt flush

    Iīm using spring aop transactions, my data is persisted correctly but the one-to-many relationships in the session cache arenīt synchronized with database when the transaction is commited, the session isnīt flushed.


    transaction configuration:

    Code:
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    	    <tx:attributes>
    	      <tx:method name="excluir*" read-only="false" propagation="REQUIRED" isolation="READ_COMMITTED" />
    	      <tx:method name="incluir*" read-only="false" propagation="REQUIRED" isolation="READ_COMMITTED"/>
    	      <tx:method name="alterar*" read-only="false" propagation="REQUIRED" isolation="READ_COMMITTED"/>            
    	      <tx:method name="*" read-only="true" propagation="SUPPORTS" isolation="DEFAULT"/>                 
    	    </tx:attributes>
    	</tx:advice>
    PS.: The hibernate mapping is ok, i already used in another application.

  • #2
    Originally posted by jdamasceno View Post
    Iīm using spring aop transactions, my data is persisted correctly but the one-to-many relationships in the session cache arenīt synchronized with database when the transaction is commited, the session isnīt flushed.
    That doesn't really make sense....if the "data is persisted correctly" than the session is being flushed....

    If you modify the parent and modify an existing child (or add a new child) are you saying the parent's modifications are persisted, but not the childs?

    Are you sure the cascade settings are correct on the one to many relationship?

    Comment


    • #3
      Thanx for your reply yatesco, The scenario is:
      • i instanced the FatherObject with only the id
      • created the SonObject
      • set the relationships
      • persist SonObject in the database (The data is ok)

      When i get the FatherObject of SonObject from database, all the fields of FatherObject but the id is null, itīs like the cache session isnīt flushed after commit.

      Comment


      • #4
        I am not sure what you were expecting to happen.....

        Did the FatherObject already exist in the DB and you were therefore expecting the persistent FatherObject to be used instead of the one you created?

        Also, how did you persist the SonObject? If that was in the same transaction then the transaction had to have been flushed

        Can you post the method which does all of this please.

        Comment


        • #5
          Originally posted by yatesco View Post

          Did the FatherObject already exist in the DB and you were therefore expecting the persistent FatherObject to be used instead of the one you created?
          Yes, itīs it.

          The code:

          Code:
          	Father father = new Father();
          	father.setId(3);
          	
          	son.setFather(father);
          	
          	// Persist serie object
          	// the transaction is executed 
          	sonDAO.insert(son);
          	
          	Collection<Son> sons = sonDAO.listAll();
          	
          	for (Son son : sons) {
          		// The son object persisted in this piece of code has 
          		// getFather().getName() empty but in database is ok.  
          		System.out.println(son.getFather().getName());
          	}

          Comment


          • #6
            Is there something wrong in this code?

            Comment


            • #7
              Sorry Juan, was away from the desk.

              Yes, the problem is that you are not refreshing the Father object, so whilst Hibernate will create the association to your existing Father object, it will *not* update your POJO Father until you ask it to.

              If you did a session.evict(son) (which will evict the Father as well) then the loop would work correctly. Because the Son and Father are in memory (you created them ) Hibernate will not magically replace them with the persistent objects.

              Comment


              • #8
                Thanx Colin, it works!

                is better query the father object before insert or call session.evict(son)?

                Comment


                • #9
                  I would suggest maybe in your code snippet...yes.

                  I notice you are not defining the Father->Son relationship in your POJO, rather you are using a DAO....just curious but why? Why not let Hibernate manage that relationship?

                  Comment

                  Working...
                  X