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();
它將從數據庫中刪除對象。
調用addProduct()和deleteProductByProdIdAndAmount()的代碼在哪裏?這應該被標記爲@Transactional ....並且因爲您使用的是Spring,所以不需要調用commit()或rollback()。 –
是啊**我在界面**中有註釋事務。而且我知道春天應該爲我做很多工作。 –