休眠交易

时间:2012-05-31 12:00:43

标签: hibernate transactions

我遇到了hibernate和事务的问题。

我想删除对象表单DB。

@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();

它将从DB中删除对象。

1 个答案:

答案 0 :(得分:0)

在删除之前,您是否从持久层获取了cart?您可能还希望将事务划分移至服务层或客户端代码。例如:

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