I'm having big problems with this so I was hoping somebody could help. I have been looking in the archives the past two days and even though I've found similar problems, I couldn't find the same one as this and definitely haven't found any answers. :(
My aim was to have a PROJECT table and a PERSON table with a foreign key to PROJECT. I set the cascade option to 'all' since I wanted all deletes to project, to cascade to PERSON and delete the appropriate record. Instead, I am getting the exception printed out below. The wierd thing is that if I delete a person record, it will delete the associated project record, unless there are more than one project records associated in which case it will throw an exception. That is definitely not the behaviour I was expecting. Something must be wrong with my mapping file, I don't know what else it could be?
If I replace the "many-to-one" in the PERSON mapping to a "one-to-one", what happens is my app will happily delete a project without complaining or throwing exceptions, but it won't delete any associated person records..
Does anybody know why this is happening?
Hibernate version: Hibernate 3.1
Mapping documents:
<hibernate-mapping >
<class name="Person" table="PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" column="PERSON_NAME"/>
<many-to-one name="project" class="Project" column="PROJECT_ID"
lazy="proxy" cascade="all" />
</class>
</hibernate-mapping>
<hibernate-mapping >
<class name="Project" table="PROJECT">
<id name="projectId" column="PROJECT_ID">
<generator class="native"/>
</id>
<property name="noOfStaff" type="integer" column="STAFF_NUMBER"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
Session session =HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
Person person = (Person)session.load(Person.class, new Integer(personId));
session.delete(person);
tx.commit();
session.close();
Full stack trace of any exception that occurs:
[java] Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
[java] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:908)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[java] at tpac.ModelManager.deletePerson(ModelManager.java:162)
[java] at tpac.ModelManager.main(ModelManager.java:66)
[java] Caused by: java.sql.BatchUpdateException: failed batch
[java] at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
[java] at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
[java] at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
[java] at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
[java] ... 9 more
[java] Java Result: 1
Name and version of the database you are using: HSQLDB - downloaded from Hibernate CVS