Announcement Announcement Module
Collapse
No announcement yet.
Issue when trying to save to the database. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue when trying to save to the database.

    Hi Guys,
    Quick Question on this one.

    I am using Spring to manage hibernate for me.

    I try and save an object but it doest persist to the database. i get the assigned object id by calling .getId();

    P.s Used App Fuse as my basic example, assumed if that worked then...

    Code:
    Vat v = new Vat();
    v.setDescription("This is a spring vat");
    v.setRate(199.99);
    try {
           vatService.saveVat(v);
    	
      } catch (DataAccessException daex) {
         daex.printStackTrace();
    }
    Dao Impl Code
    
     */
    	public void saveVat(Vat v) {
    		getHibernateTemplate().saveOrUpdate(v);
    		System.out.println("Assigned Id :"+v.getId());
    		getHibernateTemplate().flush();
    		
    	}
    Set up:
    Spring 2.1
    hibernate 3
    My sql 4

    Hibernate Database connection details.

    Code:
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
              <property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property>
              <property name="url"><value>jdbc&#58;mysql&#58;//localhost/db</value></property>
              <property name="username"><value>root</value></property>
              <property name="password"><value></value></property>
             <property name="defaultAutoCommit"><value>false</value></property>
    	</bean>
    Console Output

    Code:
    5988 &#91;main&#93; INFO support.SQLErrorCodesFactory  - SQLErrorCodes loaded&#58; &#91;Sybase, MS-SQL, MySQL, HSQL, Oracle, DB2, PostgreSQL, Informix&#93;
    Hibernate&#58; insert into vat &#40;DESCRIPTION, RATE, ID&#41; values &#40;?, ?, ?&#41;
    Assigned Id &#58;2555905

  • #2
    You set defaultAutoCommit to false. Do you have a transaction around this that would commit the save?

    Comment


    • #3
      If i spend the same amount of time reading Chapter 5 of the Spring book as i did writing the last post i could have answered my own questions.

      Also i just looked back at an earlier post and saw the suggestion from Rodd.

      I just added the following to my Confg files.

      Stolen from App Fuse
      Code:
       <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
          <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
              <property name="sessionFactory"><ref local="sessionFactory"/></property>
          </bean>
      
      
           <!-- Transaction template for Managers, from&#58;
               http&#58;//blog.exis.com/colin/archives/2004/07/31/concise-transaction-definitions-spring-11/ -->
          <bean id="txProxyTemplate" abstract="true"
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              <property name="transactionManager"><ref bean="transactionManager"/></property>
              <property name="transactionAttributes">
                  <props>
                      <prop key="save*">PROPAGATION_REQUIRED</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                  </props>
              </property>
          </bean>

      Then changed.

      Code:
      <bean id="VatDao" parent="txProxyTemplate">
      From

      Code:
      <bean id="VatDao" class="org.springframework.aop.framework.ProxyFactoryBean">
      I assume this the right approach to take?

      Comment


      • #4
        Code:
        <bean id="VatDao" parent="txProxyTemplate">
        Normally you use the 'parent' because you don't want to write the same definition more then once. Even if you want to rewrite the definition you can reuse the parent (there are more details in the Transaction chapter for the Reference Documentation).

        Comment

        Working...
        X