2014-11-06 51 views
0

我創建了一個簡單的Spring應用,從那裏有賬號(主鍵),帳戶類型,平衡春天Hibernate的交易:數據沒有得到保存

的賬目表時,曾經我嘗試更新數據沒有發生,它仍然顯示出舊的價值。

這是我的配置文件。

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

    <context:annotation-config/> 

    <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/jlcindiadb"/> 
     <property name="username" value="root"/> 
     <property name="password" value="123"/> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

     <property name="dataSource" ref="dataSource"/>  
     <property name="mappingResources"> 
      <list> 
       <value>com/jlcindia/spring/Account.hbm.xml</value> 
      </list> 
     </property> 
    <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto" >update</prop> 
      </props> 
     </property> 
    </bean>   

     <bean id="hibernateTemp" class="org.springframework.orm.hibernate3.HibernateTemplate" 
        autowire="constructor"/>  

     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" autowire="constructor"/> 

     <bean id="accdao" class="com.jlcindia.spring.HibernateAccountDAO"/>      
     <bean id="as" class="com.jlcindia.spring.AccountServiceImpl"/> 

    </beans>  

用於存儲函數的代碼片段。

public void deposit(int accno, double amt) { 
    TransactionDefinition txDef= new DefaultTransactionDefinition(); 
    TransactionStatus ts= txManager.getTransaction(txDef); 
    Account acc=htemp.load(Account.class,accno,LockMode.READ); 
    double cbal=acc.getBal();  
    acc.setBal(cbal+amt); 
    htemp.update(acc); 
    txManager.commit(ts);  
} 

最初我的表包含以下記錄:

  Account  Account Balance 
      No   Type 

Account1: 101   SA  1000 

Account2: 102   SA  2000 

Account3: 103   CA  3000 

現在就更新數據。即在存入數據後,它仍顯示相同的餘額。 爲什麼這樣的行爲?請解釋..

更新:

包含存款功能的類。

import org.hibernate.LockMode; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.orm.hibernate3.HibernateTemplate; 
import org.springframework.orm.hibernate3.HibernateTransactionManager; 
import org.springframework.transaction.TransactionDefinition; 
import org.springframework.transaction.TransactionStatus; 
import org.springframework.transaction.support.DefaultTransactionDefinition; 

public class HibernateAccountDAO implements AccountDAO{ 

@Autowired 
HibernateTemplate htemp=null; 

@Autowired 
HibernateTransactionManager txManager=null; 

@Override 
public void deposit(int accno, double amt) { 
    TransactionDefinition txDef= new DefaultTransactionDefinition(); 
    TransactionStatus ts= txManager.getTransaction(txDef); 
    Account acc=htemp.load(Account.class,accno,LockMode.READ); 
    double cbal=acc.getBal(); 
    //int cbal=(int)bal; 

    acc.setBal(cbal+amt); 
    htemp.update(acc);  
    txManager.commit(ts); 
    //System.out.println("Acc get Account number "+acc.getAccno()); 
    //System.out.println("Acc get Account type "+acc.getAtype()); 
    //System.out.println("Acc get Balance "+acc.getBal()); 
} 


@Override 
public void withdraw(int accno, double amt) { 
    TransactionDefinition txDef= new DefaultTransactionDefinition(); 
    TransactionStatus ts= txManager.getTransaction(txDef); 
    Account acc=htemp.load(Account.class,accno,LockMode.READ); 
    double cbal=acc.getBal(); 
    if(cbal>500+amt){ 
     acc.setBal(cbal-amt); 
     htemp.update(acc);   
    }else{ 
     throw new InSufficientFundsException();   
    } 
    txManager.commit(ts); 

} 

@Override 
public void fundsTransfer(int saccno, int daccno, double amt) { 
    TransactionDefinition txDef= new DefaultTransactionDefinition(); 
    TransactionStatus ts= txManager.getTransaction(txDef); 
    try{ 
    Account acc1=htemp.load(Account.class,daccno,LockMode.READ); 
    double dcbal=acc1.getBal(); 
    acc1.setBal(dcbal+amt); 
    htemp.update(acc1); 

    Account acc2=htemp.load(Account.class,saccno,LockMode.READ); 
    double scbal=acc2.getBal(); 
    if(scbal>=500+amt){ 
     acc2.setBal(scbal-amt);   
     htemp.update(acc2); 
    }else{ 
     throw new InSufficientFundsException(); 
    } 
    txManager.commit(ts); 

    }catch(Exception e){ 
     txManager.rollback(ts); 
     e.printStackTrace(); 
    } 
} 

@Override 
public double getBalance(int accno) { 
    TransactionDefinition txDef= new DefaultTransactionDefinition(); 
    TransactionStatus ts= txManager.getTransaction(txDef); 
    Account acc=htemp.load(Account.class,accno,LockMode.READ); 
    double cbal=acc.getBal(); 
    return cbal;   
} 

}

回答

0

好像你還沒有提供會話工廠冬眠模板和事務管理。嘗試通過修改特定的xml部分來再次測試,如下所示? :

<bean id="hibernateTemp" class="org.springframework.orm.hibernate3.HibernateTemplate" 
     autowire="constructor"/> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean>     

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
     autowire="constructor"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 

我的模擬片段:

Resource r=new ClassPathResource("applicationContext.xml"); 
BeanFactory factory=new XmlBeanFactory(r); 
HibernateTemplate template = (HibernateTemplate) factory.getBean("template"); 
HibernateTransactionManager txManager = (HibernateTransactionManager) factory.getBean ("txManager"); 
TransactionDefinition txDef= new DefaultTransactionDefinition(); 
TransactionStatus ts= txManager.getTransaction(txDef); 
Employee acc=template.load(Employee.class,114,LockMode.READ);  
acc.setName("varun"); 
template.update(acc); 
txManager.commit(ts); 
+0

感謝表現出興趣,我實現了上述建議。但它仍然給出了相同的結果。這是數據仍然沒有得到保存。 – User27854 2014-11-06 04:56:56

+0

如果會話工廠bean被調用'sessionFactory',Hibernate會自動選擇它。只有當你的bean被稱爲不同的東西,你必須指定它。 – JamesENL 2014-11-06 05:25:59

+0

@JamesMassey,好吧,我的問題仍然存在,任何想法如何超過這一點。 – User27854 2014-11-06 06:52:40