-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problem with deleting in a many-to-one association
PostPosted: Mon Dec 05, 2005 2:53 am 
Newbie

Joined: Mon Dec 05, 2005 2:30 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 4:25 am 
Newbie

Joined: Sun Oct 16, 2005 11:38 pm
Posts: 5
Hi RollingStone!
Have you tried replacing session.load() by session.get() yet?
In addition, you should check constraint cascading of your db and review logging text above exception stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 5:41 am 
Newbie

Joined: Mon Dec 05, 2005 2:30 am
Posts: 3
khanhtq wrote:
Hi RollingStone!
Have you tried replacing session.load() by session.get() yet?
In addition, you should check constraint cascading of your db and review logging text above exception stack trace.


No, I haven't tried that, but I definitely will tomorrow when I get back to work. Thanks, and I really hope this makes a difference. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 8:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
I set the cascade option to 'all' since I wanted all deletes to project, to cascade to PERSON and delete the appropriate record

But that's not what you do. You delete a Person, not a Project.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.