2012-05-31 115 views
0

我有休眠和事务的问题。休眠事务

我想删除对象表单数据库。

@Transactional 
public boolean addProduct(Cart cart) { 
    try { 
     sessionFactory.getCurrentSession().save(cart); 
     return true; 
    } catch (HibernateException e) { 
     LOG.debug("ERROR adding product into cart" + e); 
     return false; 
    } 

} 

@Transactional 
public boolean deleteProductByProdIdAndAmount(Cart cart) {  
    sessionFactory.getCurrentSession().delete(cart); 
    return true; 
} 

它完美地添加对象,但它不会删除它,我不会获得任何异常。如果我写

sessionFactory.getCurrentSession().getTransaction().commit(); 

它会告诉我一个异常事件没有成功启动,但它会删除一个对象。下面是我的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" 
    default-lazy-init="true"> 

<tx:annotation-driven transaction-manager="txManager" /> 


<bean id="configProperties" class="com.dataart.masternoy.utils.PropertiesUtil"> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath:/config.properties</value> 
      <value>classpath:/jdbc.properties</value> 
     </list> 
    </property> 
</bean> 

<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/shop" /> 
    <property name="username" value="root" /> 
    <property name="password" value="root" /> 
</bean> 

<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> 
    <constructor-arg ref="dataSource" /> 
</bean> 

<bean id="txManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

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


<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
    lazy-init="true"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernateConfig.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> 
      <prop key="hibernate.connection.charSet">UTF-8</prop> 


     </props> 
    </property> 
</bean> 

如果我写

sessionFactory.getCurrentSession().createQuery("delete from Cart where order_id = 77 and product_id = 9").executeUpdate(); 

它将从数据库中删除对象。

+1

调用addProduct()和deleteProductByProdIdAndAmount()的代码在哪里?这应该被标记为@Transactional ....并且因为您使用的是Spring,所以不需要调用commit()或rollback()。 –

+0

是啊**我在界面**中有注释事务。而且我知道春天应该为我做很多工作。 –

回答

0

在删除之前,您是从持久层获取cart?您可能还想将您的事务划分移出到服务层或您的客户端代码。示例:

@Transactional 
public void deleteCart(long id) { 
    Cart cart = this.cartDao.getCart(id); 
    this.cartDao.deleteProductByProdIdAndAmount(cart); 
}