Strut+Spring+Hibernate应用,在一个Session中,先新增后删除,出现:Could not execute JDBC batch update错误。
若林林 2008-04-02 10:39:29 程序是基于Strut+Spring+Hibernate。
在一个Session中,先新增后删除,出现,Could not execute JDBC batch update错误。
错误:
org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access; nested exception is org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
..............
下面是程序:
-----------------------------------------------------------------------------
public class TestOrderServiceSpringImpl extends BaseTestCase {
public void testFindOrderById() {
IOrderService service = (IOrderService) super.getBean("orderService");
try {
Order order = service.findOrderById(1);
assertNotNull(order);
} catch (OrderException e) {
getLog().error(e);
e.printStackTrace();
}
}
public void testSaveNewOrder() throws Exception {
IOrderService service = (IOrderService) super.getBean("orderService");
try {
Order order = new Order();
order.setUserName("userName");
service.saveNewOrder(order);
int id = order.getId();
//super.tearDown();
//super.setUp(); 加入这两条,后面的Hibernate操作在另一个session中执先就可以
service.deleteOrder(order); //执先这里出错。
order = null;
order = service.findOrderById(id);
assertNull(order);
} catch (OrderException e) {
getLog().error(e);
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------
public class BaseTestCase extends TestCase {
private static Log log = LogFactory.getLog(BaseTestCase.class);
private ApplicationContext ac;
SessionFactory sessionFactory;
protected void setUp() throws Exception {
super.setUp();
ac = new FileSystemXmlApplicationContext(
"D:/workspace/code/order/WebRoot/WEB-INF/config/spring/applicationContext.xml");
sessionFactory = (SessionFactory) getBean("mySessionFactory");
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(s));
}
protected void sessionFlush() throws Exception {
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
}
protected void tearDown() throws Exception {
super.tearDown();
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(s);
}
protected Object getBean(String name) {
return ac.getBean(name);
}
public static Log getLog() {
return log;
}
}