|
In many-to-many inverse end , i delete a persistent object failed. I find hibernate only delete record in inverse end table but relation table, while there is a foreign key in relation table, so the exception is thrown .
How can i fix the problem. Please help me . THANKS
hibernate 2.1.6:
code: /** * * @hibernate.class * table = "a" */ public class A implements Serializable { private Long id;
private String name;
private List Bs = new ArrayList();
/** * * @return * @hibernate.bag * cascade = "save-update" * lazy = "true" * table = "a_b" * @hibernate.collection-many-to-many * class = "com.cofo.ebp.domain.x.po.B" * column = "b_id" * @hibernate.collection-key * column = "a_id" */ public List getBs() { return Bs; }
/** * * @return * @hibernate.id * generator-class = "native" * unsaved-value = "null" */ public Long getId() { return id; }
/** * * @return * @hibernate.property * */ public String getName() { return name; }
public void setBs(List bs) { Bs = bs; }
public void setId(Long id) { this.id = id; }
public void setName(String name) { this.name = name; } public void addB(B b){ Bs.add(b); b.getAs().add(this); } public void removeB(B b){ Bs.remove(b); b.getAs().remove(this); } }
/** * * @hibernate.class * table = "b" */ public class B implements Serializable { private Long id;
private String name;
private List As = new ArrayList();
/** * * @return * * @hibernate.bag * cascade = "save-update" * table = "a_b" * inverse = "true" * lazy = "true" * @hibernate.collection-many-to-many * class = "com.cofo.ebp.domain.x.po.A" * column = "a_id" * @hibernate.collection-key * column = "b_id" */ public List getAs() { return As; } /** * * @return * * @hibernate.id * generator-class = "native" * unsaved-value = "null" */ public Long getId() { return id; }
/** * * @return * * @hibernate.property */ public String getName() { return name; }
public void setAs(List as) { As = as; }
public void setId(Long id) { this.id = id; }
public void setName(String name) { this.name = name; } }
Code between sessionFactory.openSession() and session.close(): i use BDao and HibernateUtil in book "hibernate in action"
BDao bDao = new BDao(); B b = bDao.getById(bid, false); bDao.makeTransient(b); HibernateUtil.commitTransaction(); HibernateUtil.closeSession();
Full stack trace of any exception that occurs:
There was 1 error: 1) testDeleteB(com.cofo.ebp.domain.test.x.ABTest)com.cofo.ebp.domain.exceptions.InfrastructureException: net.sf.hibernate.JDBCException: Could not execute JDBC batch update at com.cofo.ebp.domain.HibernateUtil.commitTransaction(HibernateUtil.java:208) at com.cofo.ebp.domain.test.x.ABTest.testDeleteB(ABTest.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at com.cofo.ebp.domain.test.EBPBaseTestCase.runTest(EBPBaseTestCase.java:50) at com.cofo.ebp.domain.test.x.ABTest.main(ABTest.java:21) Caused by: net.sf.hibernate.JDBCException: Could not execute JDBC batch update at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:133) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2376) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240) at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61) at com.cofo.ebp.domain.HibernateUtil.commitTransaction(HibernateUtil.java:203) ... 19 more Caused by: java.sql.BatchUpdateException: failed batch at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source) at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source) at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54) at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126) ... 24 more
Name and version of the database you are using: hsqldb_1_7_2_4
|