Announcement Announcement Module
Collapse
No announcement yet.
merge has no effect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • merge has no effect

    Hi,

    I create a small test to check the merge functionality.

    I basically:

    1) retreive in one session (via JpaTemplate) an object.
    2) Modify the object
    3) from another session (still via JpaTemplate) merge the object.

    When I check the database the object is not changed?

    Here is a part of the code:

    Code:
    	public void testUpdateColorStone() {
    		System.out.println("sssss START ssss");
    		TestObject testObject = (TestObject) testDAO.findByReference("Test");
    		System.out.println(testObject .getComments());
    		testObject .setComments("last comments");
    		System.out.println(testObject .getComments());
    		testDAO.merge(testObject );
    	}
    I don't see in the log file any update statement!

    My DAO use the entityManager to create a session.

    [main] DEBUG org.springframework.orm.jpa.JpaTemplate - Creating new EntityManager for JpaTemplate execution
    [main] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 4743565661175808

    When I use the merge in the same session/transaction as the finder, it works but not with different session. Isn't it the goal of the merge to reattach an object and save it?

    Any Idea?

    Thanks
    Dominique

  • #2
    Yes it is - make sure that both sessions are read-write and the transaction in which they are wrapped commits properly. If that still doesn't work and the Hibernate logs don't provide any help, go try their forums.
    The HibernateEM hasn't been updated in a while and there are a number of bug fixes pending.

    Comment


    • #3
      Thanks,

      My sessions are in read-write and commit, because I'm able to insert new object.

      I'll make a test in pure Hibernate (without spring) to verify it.

      Thanks for your advice.
      Dominique

      Comment


      • #4
        I made a test with Hibernate only and the update is done:

        Code:
        	
        public void testUpdateColorTestObjectEM() {
                     EntityManagerFactory emf = (EntityManagerFactory) context.getBean("entityManagerFactory");
                     EntityManager em = emf.createEntityManager();
        	EntityTransaction tx = em.getTransaction();
        	tx.begin();
        	System.out.println("sssss START ssss");
        	Query q = em.createQuery("from TestObject as testObject where testObject.reference='MyColorTestObject'");
        	List list = q.getResultList();
        	TestObject testObject = (TestObject) list.get(0);
        	tx.commit();
        	em.close();
        	em = emf.createEntityManager();
        	
        	System.out.println(testObject.getComments());
        	testObject.setComments("last comments4");
        	System.out.println(testObject.getComments());
        	tx = em.getTransaction();
        	tx.begin();
        	em.merge(testObject);
        	tx.commit();
        	em.close();
        	}
        so then there is an issue with the config of JpaTemplate here is my JpaTemplate bean definiation

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xsi:schemaLocation="
               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        
        	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        		<property name="persistenceUnitName" value="model"/>
        	</bean>
        	
         	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            	<property name="entityManagerFactory" ref="entityManagerFactory"/>
          	</bean>
        
          	<aop:config>
            	<aop:pointcut id="productServiceMethods" expression="execution(* test.*.*.*.*(..))"/>
            	<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
          	</aop:config>
        
        	<tx:advice id="txAdvice" transaction-manager="txManager">
            	<tx:attributes>
              		<tx:method name="*"  propagation="REQUIRES_NEW"/>
            	</tx:attributes>
        	</tx:advice>
        </beans>
        With JpaTemplate I have in the log:

        main] DEBUG org.hibernate.loader.Loader - done entity load
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.springframework.orm.jpa.JpaTemplate - Closing new EntityManager after JPA template execution
        [main] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
        [main] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Bean with name 'entityManagerFactory' is a factory bean
        [main] INFO org.hibernate.impl.SessionFactoryImpl - closing
        [main] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - cleaning up connection pool: jdbc:mysql://127.0.0.1/gems


        With EM I have:

        [main] DEBUG org.hibernate.loader.Loader - done entity load
        [main] DEBUG org.hibernate.transaction.JDBCTransaction - commit
        [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - processing flush-time cascades
        [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - dirty checking collections
        [main] DEBUG org.hibernate.engine.Collections - Collection found: [test.entity.model.TestObject.treatments#402881830d a37baf010da37bb6540005], was: [test.entity.model.TestObject.treatments#402881830d a37baf010da37bb6540005] (initialized)
        [main] DEBUG org.hibernate.engine.Collections - Collection found: [test.entity.model.TestObject.certificates#40288183 0da37baf010da37bb6540005], was: [test.entity.model.TestObject.certificates#40288183 0da37baf010da37bb6540005] (uninitialized)
        [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - Flushed: 0 insertions, 1 updates, 0 deletions to 6 objects
        [main] DEBUG org.hibernate.event.def.AbstractFlushingEventListe ner - Flushed: 0 (re)creations, 0 updates, 0 removals to 2 collections
        [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
        [main] DEBUG org.hibernate.SQL - update TestObject set reference=? where id=?
        Hibernate: update TestObject set reference where id=?
        [main] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
        [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
        [main] DEBUG org.hibernate.transaction.JDBCTransaction - re-enabling autocommit
        [main] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
        [main] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
        [main] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'entityManagerFactory'
        [main] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Bean with name 'entityManagerFactory' is a factory bean
        [main] INFO org.hibernate.impl.SessionFactoryImpl - closing
        [main] INFO org.hibernate.connection.DriverManagerConnectionPr ovider - cleaning up connection pool: jdbc:mysql://127.0.0.1/gems

        It seems that some Listener are not started or triggered with the JpaTemplate!

        If somebody has any idea, it will be welcome.

        Thanks
        Dominique
        Last edited by Dominique; Sep 13th, 2006, 11:31 AM.

        Comment


        • #5
          I found the problem, my aop:config expression wasn't correct and then no transaction was created around the merge method which was a method in the super class of my stoneDAO.

          Now it works, thanks for your advise Costin.

          Comment

          Working...
          X