我需要一些帮助。我在使用休眠和弹簧的事务时遇到问题。我正在尝试填写 mysql 中的文件表。我的第一个插入效果很好,第二个不起作用(但这是正常的......)。但是第一个插入的数据仍然存在于表中。这不符合交易的概念。我可以吗?
我认为第二次尝试在数据库中插入损坏的数据时应该执行回滚(损坏的数据是指与字段约束不匹配的数据) insert 应该是回滚的,并且表中不应存在任何数据。或者事实并非如此。第一个数据,来自第一个插入仍然在表中。它不应该仍然存在吗?
我试图查看已检查/未检查的异常内容,@transactionnal 的错误配置但没有成功...
如果你有任何想法...
谢谢 !!
主.java:
public static void main( String[] args ) throws Exception{
ApplicationContext appContext = new ClassPathXmlApplicationContext( "classpath:webConfiguration/applicationContext.xml");
FileBo aFileBo = (FileBo) appContext.getBean("fileBo");
// Data are ok, there is an insert.
File aFile = File (6778687,".bam");
aFileBo.save(aFile);
// Error is produce here because the ".pdf" value is not tolerated in the enum field we want to fill up. No data is inserted. But a rollback should be done and data inserted before should be erased ?
File anAnotherFile = File (6567887,".pdf");
aFileBo.save(anAnotherFile);
}
FileBoImpl.java
public class FileBoImpl implements FileBo{
FileDao fileDao;
public FileDao getFileDao() {
return fileDao;
}
public void setFileDao(FileDao fileDao) {
this.fileDao = fileDao;
}
@Transactional// one transaction for multiple operations
public void save(com.clb.genomic.lyon.model.File aFile) {
fileDao.save(aFile);
}
休眠.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd " >
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
...
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
在 applicationContext.xml 中调用的 Beans.xml 也带有 Hibernate.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- Data Access Object -->
<bean id="fileDao" class="com.clb.genomic.lyon.dao.FileDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Business Object -->
<bean id="fileBo" class="com.clb.genomic.lyon.bo.FileBoImpl" >
<property name="fileDao" ref="fileDao"></property>
</bean>
</beans>