Hibernate 休眠事务

Hibernate 休眠事务,hibernate,transactions,Hibernate,Transactions,我对hibernate和事务有问题 我想从数据库中删除对象 @Transactional public boolean addProduct(Cart cart) { try { sessionFactory.getCurrentSession().save(cart); return true; } catch (HibernateException e) { LOG.debug("ERROR adding product int

我对hibernate和事务有问题

我想从数据库中删除对象

@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();
sessionFactory.getCurrentSession().createQuery("delete from Cart where order_id = 77 and product_id = 9").executeUpdate();
它将向我显示一个异常,即事务未成功启动,但它将删除一个对象。这是我的配置文件:

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

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

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

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

其中是调用addProduct()和DeleteProductByProdAndAmount()的代码?这应该标记为@Transactional…,因为您使用的是Spring,所以不需要调用commit()或rollback()。我知道春天应该为我做很多工作。