-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-one association
PostPosted: Sun Oct 01, 2006 6:07 pm 
Newbie

Joined: Sun Oct 01, 2006 5:31 pm
Posts: 3
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!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 01, 2006 8:24 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
That exception seems to point to other problems. For example you may be trying to run database commands outside of a session/transaction?

For the second part of your question, one possible is to use foreign key constraints if the database supports them. For HSQLDB look under constraint definition in this page
http://www.hsqldb.org/doc/guide/ch09.ht ... le-section


Not sure if this helps ...

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 6:20 am 
Newbie

Joined: Sun Oct 01, 2006 5:31 pm
Posts: 3
Thank you Marius!

I got the problem! The constraints work well, but hsql driver doesn't. The driver simple doesn't give any information about the problem that occured while it's trying to delete a parent row. I noticed that Hibernate want to execute the statement in batch and I found the place in the code of the hsql driver which raises the javax.sql.BatchUpdateException. The driver doesn't give any information, why the statement is failed.


I checked it via JDBC as well. A tried to delete a parent row by
PreparedStatement.executeBatch and I got the same. But if I use PreparedStatement.execute then i get the expected exception.

So the problem was caused by the hsql driver.....

by


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 10:45 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Nice, thanks for sharing. So the exception was due to constraints being enforced or some other problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 02, 2006 10:54 am 
Newbie

Joined: Sun Oct 01, 2006 5:31 pm
Posts: 3
The exception was occured because of foreign key integrity violation, but the hsql driver simple said: failed batch update etc. So the exception was due to the constrainst.

Bye


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.