簡體   English   中英

Spring Hibernate Transaction:未保存數據

[英]Spring Hibernate Transaction: Data is not getting saved

我創建了一個簡單的spring應用程序,其中有一個帶有帳號(主鍵),帳戶類型,余額的帳戶表

每當我嘗試更新未發生的數據時,它仍然顯示舊值。

這是我的配置文件。

<?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;        
}

}

似乎您沒有提供會話工廠來休眠模板和事務管理器。 嘗試通過修改特定的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM