Announcement Announcement Module
Collapse
No announcement yet.
Hibernate + Spring does not write to db Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate + Spring does not write to db

    Hello

    I pretty much followed the Springtraining application within the Spring in Action book, everything including db reading works fine. Unfortunately the application does not write to the db. Further it simple ignores the show_sql property.

    Here is my hibernate propertys file

    Code:
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
       "http&#58;//hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
       <session-factory>
       
          
          <property name="show_sql">true</property>
          <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
         
    
       </session-factory>
    </hibernate-configuration>
    here is the relevant part of my dispatcher-servlet.xml

    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		
        
    		
    		<property name="dataSource">
    			<ref bean="dataSource"/>
    		</property>
    		
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>classpath&#58;/com/praemed/database/data</value>
    			</list>
    		
    		</property>
    	</bean>
    	
    	<!-- Services 
    
    	-->
    	
    <bean id="regionServiceTarget" 
          class="com.praemed.service.RegionServiceImpl">
        <constructor-arg><ref bean="regionDao"/></constructor-arg>
      </bean>
      
      	
      
    
      <!-- TRANSACTION SUPPORT -->
      
      <bean id="transactionManager" 
    	    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	  <property name="sessionFactory">                                  
    	    <ref bean="sessionFactory"/>
    	  </property>
    	</bean>
    
    
      <bean id="regionService"
    	    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	  <property name="target"><ref bean="regionServiceTarget"/></property>
        <property name="transactionManager">
          <ref bean="transactionManager"/>
        </property>
        
        <property name="transactionAttributes">
          <props>
            <prop key="createRegion">
              PROPAGATION_REQUIRED,ISOLATION_DEFAULT
            </prop>
          </props>
        </property>
      </bean>  
    
      
    	<!-- DAO classes 
    
    	-->
    	
    	<bean id="hibernateTemplate"
    	class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    	</bean>
    	
    	<bean id="criteriaDao" class="com.praemed.database.dao.CriteriaDaoHibernate">
    
    		<!--property name="hibernateTemplate">
    			<ref bean="hibernateTemplate"/>
    		</property-->
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    		
    	</bean>
    	
    	<bean id="regionDao" class="com.praemed.database.dao.RegionDaoHibernate">
    
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    		
    	</bean>
    	
    	<bean id="invasivetyLevelDao" class="com.praemed.database.dao.InvasivetyLevelDaoHibernate">
    
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
    		
    	</bean>
    here is my DAOClass

    Code:
    public class RegionDaoHibernate extends HibernateDaoSupport implements RegionDao &#123;
    
        
        protected final Log LOGGER = LogFactory.getLog&#40;getClass&#40;&#41;&#41;; 
        /* &#40;non-Javadoc&#41;
         * @see com.praemed.database.dao.RegionDao#findById&#40;java.lang.String&#41;
         */
        public Region findById&#40;final String id&#41; &#123;
    
           
              return &#40;Region&#41;getHibernateTemplate&#40;&#41;.load&#40;Region.class,new Long&#40;id&#41;&#41;;
               
          
                   
        &#125;
    
        /* &#40;non-Javadoc&#41;
         * @see com.praemed.database.dao.RegionDao#create&#40;com.praemed.database.data.Region&#41;
         */
        public void create&#40;Region region&#41; &#123;
            
            
             LOGGER.info&#40;"Persisting Region&#58;"+region.getId&#40;&#41;+" "+region.getName&#40;&#41;&#41;;
           
            
            getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;region&#41;;
            	
            LOGGER.info&#40;"Region persisted"&#41;;
            
        &#125;
    
        /* &#40;non-Javadoc&#41;
         * @see com.praemed.database.dao.RegionDao#getAllRegions&#40;&#41;
         */
        public List getAllRegions&#40;&#41; &#123;
            
           
            return getHibernateTemplate&#40;&#41;.loadAll&#40;Region.class&#41;;
            
        &#125;
        
    &#125;
    Thats what the logfile tells me

    Code:
    2005-06-20 22&#58;13&#58;30,426 INFO &#91;com.praemed.mvc.AddRegionController&#93; - **************************** Creating Region
    2005-06-20 22&#58;13&#58;30,426 INFO &#91;com.praemed.database.dao.RegionDaoHibernate&#93; - Persisting Region&#58;0 lkjh
    2005-06-20 22&#58;13&#58;30,436 INFO &#91;com.praemed.database.dao.RegionDaoHibernate&#93; - Region persisted
    2005-06-20 22&#58;13&#58;30,446 INFO &#91;com.praemed.mvc.AddRegionController&#93; - **************************** Region created.
    I tried to flush manually but it didn't help

    TIA

    Best regards

    John

  • #2
    Hey,

    You seem to have configured the transaction for the method 'createRegion':

    Code:
    <property name="transactionAttributes"> 
          <props> 
            <prop key="createRegion"> 
              PROPAGATION_REQUIRED,ISOLATION_DEFAULT 
            </prop> 
          </props> 
        </property> 
      </bean>
    The actual dao method is create, not createRegion. I don't think a transaction is being started for the dao, so it will never commit and hence flush to the database.

    Change the above prop key to use 'create' instead and see what happens.

    cheers

    Comment


    • #3
      Thanks for the quick reply.

      As far as i see the Transaction should be defined for my service and its method createRegion, so this should be correct. I tried it anyway but it didnt fix the problem.

      Any hints ?

      TIA

      Comment

      Working...
      X