Announcement Announcement Module
Collapse
No announcement yet.
Springs EntityManager.persist won't flush using JpaTransactionManager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Springs EntityManager.persist won't flush using JpaTransactionManager

    I can't seem to get EntityManager.persist to work using JpaTransactionManager in Spring. My code does not generate any errors, but it doesn't write to the database, either. It appears that managed entities within the EntityManager cache are being modified ok, but no flushing occurs to actually persist new entities, or even updated ones, using EntityManager.merge, to the database. In fact, even if I force a flush, calling
    EntityManager.flush() within the test, it still doesn't persist to the database.

    I can tell the test case is running within a transaction by logging 'before' and 'after' transaction messages.

    Does anyone have any experience with this and any ideas on the cause and possible solution.


    I've posted the relevant code below. Don't get alarmed by the "&" notation. This forum apparently complains if I use a real ampersand.

    Thanks...



    My application context for Spring includes the following bean definitions:

    Code:
    <tx:annotation-driven/>
        <tx:jta-transaction-manager/>
        <aop:aspectj-autoproxy/>
        <context:annotation-config/>
           
    <!-- define the data source -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:url="jdbc:...."
            p:driverClassName="com.sybase.jdbc3.jdbc.SybDriver"
            p:username="blahblah"
            p:password="blahblah"/>
                   
    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="persistenceXmlLocation" value="META-INF/persistence.xml"/>
            <property name="persistenceUnitName" value="myPersistenceUnit"/>
            <property name="jpaVendorAdapter">
            <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
                                    p:databasePlatform="org.eclipse.persistence.platform.database.SybasePlatform"
                                    p:showSql="true"/>
                    </property>
                    <property name="loadTimeWeaver">
                            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
                    </property>
            </bean>
           
            <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">    
                    <property name="entityManagerFactory" ref="myEmf"/>
            </bean>
           
            <!-- bean post-processor for JPA annotations -->
            <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
           
            <bean id="allCodeManager" class="com.dylt.webManager.ejb.eao.allcode.AllCodeManager" />

    My persistence.xml is as follows:

    Code:
    <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
               <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
               <mapping-file>META-INF/orm-allcode.xml</mapping-file>
               <exclude-unlisted-classes/>  
            </persistence-unit>
    </persistence>
    orm-allcode.xml contains the entity declaration:

    Code:
    <entity class="com.dylt.webManager.model.allcode.AllCode" access="FIELD">      
                    <table name="allCodes">
                    </table>
                    <attributes>
                            ...
                    </attributes>
    </entity>
    My Data Access Object is as follows:

    Code:
    public class AllCodeManager extends AllCodeEAO{
    
           &amp;PersistenceContext(unitName="myPersistenceUnit")
            protected EntityManager em;
    
            
            public void createAllCode(AllCode entity) {
                   em.persist(entity);
            }
           
           public AllCode findById(int id) {
    		return (AllCode)em.find(AllCode.class, id);
    	}
    }
    And, I have a test class to create a new AllCode entity:

    Code:
    &amp;RunWith(SpringJUnit4ClassRunner.class)
    &amp;ContextConfiguration(locations={"classpath:META-INF/applicationContext-all.xml"})
    &amp;Transactional
    
    public class TestAllCode {
           &amp;Autowired
           private AllCodeEAO allCodeManager;
    
          &amp;BeforeTransaction
    	public void beforeTransaction() {
    		System.out.println("Transaction started...");
    	}
    	
    	&amp;AfterTransaction
    	public void afterTransaction() {
    		System.out.println("Transaction finished");
    	}
     &amp;Test
     public void createAllCodeTest(){
       AllCode allcode = new AllCode();
       allcode.setId(10);
       allcode.setCategory(2);
       allCodeManager.createAllCode(allcode);
    
       Assert.assertNotNull(allCodeManager.findById(10));   
        
    }
    
    }

  • #2
    All database changes made in @Test methods are rolled back by default, unless you specify:

    Code:
    @TransactionConfiguration(defaultRollback=false)
    or

    Code:
    @Test
    @Rollback(false)
    See the chapter on Testing in the reference docs.

    Comment

    Working...
    X