Results 1 to 4 of 4

Thread: Issue when trying to save to the database.

  1. #1
    Join Date
    Apr 2005
    Location
    Galway, Ireland
    Posts
    24

    Default 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. #2
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    You set defaultAutoCommit to false. Do you have a transaction around this that would commit the save?
    --Jing Xue

  3. #3
    Join Date
    Apr 2005
    Location
    Galway, Ireland
    Posts
    24

    Default

    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?

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Similar Threads

  1. Replies: 4
    Last Post: Oct 26th, 2006, 02:20 AM
  2. Replies: 1
    Last Post: Sep 21st, 2005, 04:37 AM
  3. Replies: 1
    Last Post: Jul 18th, 2005, 12:43 PM
  4. Replies: 2
    Last Post: May 26th, 2005, 02:30 AM
  5. Replies: 1
    Last Post: May 5th, 2005, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •