I have class A and class B with the following mapping files:
(I use Hibernate 3.1 and HSQLDB 1.8.0)
Code:
<hibernate-mapping>
<class name="A">
<id name="id" column="a_id" type="integer">
<generator class="native"/>
</id>
<many-to-one name="b" column="b_id"
class="B" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="B">
<id name="id" column="b_id" type="integer">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
So it's a simple unidirectional many-to-one association.
If I try to delete an object with type B, I get the following exception:
2006-10-01 23:11:00,196 [main] WARN org.hibernate.util.JDBCExceptionReporter -
SQL Error: 0, SQLState: null
2006-10-01 23:11:00,196 [main] ERROR org.hibernate.util.JDBCExceptionReporter -
failed batch
2006-10-01 23:11:00,236 [main] ERROR org.hibernate.event.def.AbstractFlushingEve
ntListener - Could not synchronize database state with session
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.j
ava:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp
er.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio
ns(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java
:106)
at webshop.db.HibernateUtil.delete(Unknown Source)
at webshop.db.PersistentObject.delete(Unknown Source)
at test.consoleprograms.language.DeleteLanguage.main(Unknown Source)
Caused by: java.sql.BatchUpdateException: failed batch
at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.jav
a:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:
195)
In my application I would like to prevent such rows of the table B from deleting which are referenced by any rows of table A i. e. a parent row can not be deleted until it have any child rows. How can I achieve this, how can I get an appropriate exception e. g.? I think this exception above is not the right one.....
Thank you!