-->
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.  [ 6 posts ] 
Author Message
 Post subject: Exception not throw (java.sql.SQLException)
PostPosted: Fri Oct 17, 2003 1:30 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Hello,

Why some exceptions can't be tested ? I got this error :

------
Hibernate: insert into nom_user_group (user_id, group_id) values (?, ?)
03:06:32,934 WARN JDBCExceptionReporter:38 - SQL Error: -8, SQLState: 23000
03:06:32,937 ERROR JDBCExceptionReporter:46 - Integrity constraint violation: UG_G_FKEY table: NOM_GROUP in statement [insert into nom_user_group (user_id, group_id) values (30, 1)]
03:06:32,947 ERROR JDBCExceptionReporter:37 - Could not synchronize database state with session
java.sql.SQLException: Integrity constraint violation: UG_G_FKEY table: NOM_GROUP in statement [insert into nom_user_group (user_id, group_id) values (30, 1)]
at org.hsqldb.Trace.getError(Unknown Source)
at org.hsqldb.Result.<init>(Unknown Source)
at org.hsqldb.jdbcConnection.executeHSQL(Unknown Source)
at org.hsqldb.jdbcConnection.execute(Unknown Source)
at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source)
at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.collection.CollectionPersister.recreate(CollectionPersister.java:633)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2065)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
at com.estobel.model.HibernateUserDAO.insert(Unknown Source)
at com.estobel.desktop.util.DesktopSyncFacade.updateUser(Unknown Source)
-------

The code that generated the error is:
try {

myDAO.insert(object)
//insert() throws hibernateException

} catch (HibernateException e ) {
//show some message
}

Why the Integrity constraint violation does not generated a HibernteExeption ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 3:44 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
It doesn't throw a Hibernate exception, as it doesn't really have anything to do with Hibernate.

This is more to do with your Database, as Hibernate doesn't check your specific database constraints. You need to tell Hibernate these constraints in your mapping file, and then it will throw a HibernateException.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 9:16 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Thanks Brannor,

I have mapped the constraints in map file ... at least I think ... look:

--
<class name="com.estobel.model.User" table="nom_user">
.....
.....
<set name="groups" table="nom_user_group" lazy="true">
<key>
<column name="user_id" not-null="true"/>
</key>
<many-to-many class="com.estobel.model.Group">
<column name="group_id" not-null="true"/>
</many-to-many>
</set>
</class>

<class name="com.estobel.model.Group" table="nom_group">
.....
.....
<set name="users" table="nom_user_group" inverse="true" lazy="false">
<key>
<column name="group_id" not-null="true"/>
</key>
<many-to-many class="com.estobel.model.User">
<column name="user_id" not-null="true"/>
</many-to-many>
</set>
</class>
----

if I try to insert in "nom_user_group" a "group_id" that doesn't exists in "nom_group" the exception throw but not as a HibernateException ...

-----
Hibernate: insert into nom_user_group (user_id, group_id) values (?, ?)
10:13:16,498 WARN JDBCExceptionReporter:38 - SQL Error: -8, SQLState: 23000
10:13:16,498 ERROR JDBCExceptionReporter:46 - Integrity constraint violation: UG_G_FKEY table: NOM_GROUP in statement [insert into nom_user_group (user_id, group_id) values (7, 1)]
10:13:16,508 ERROR JDBCExceptionReporter:37 - Could not synchronize database state with session
java.sql.SQLException: Integrity constraint violation: UG_G_FKEY table: NOM_GROUP in statement [insert into nom_user_group (user_id, group_id) values (7, 1)]
------

Is my mapping correct ?

Thanks a lot.

Neimar


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 10:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nvolpini is right, this exception should have been wrapped into a n.s.h.JDBCException which extends Hibernate Exception.

And it actually does. Hibernate throws a JDBCException whitch nest SQLException. The message is just formatted to show SQL Exception.The logs you have are generated during creation of JDBCException.
Are you sure, you don't catch it ?

What does your myDAO.insert() really does ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 11:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
Why the Integrity constraint violation does not generated a HibernteExeption ?

It does. In particular, it will generate an exception of type net.sf.hibernate.JDBCException. This works as I use it to generate display information to the end user by parsing out the violated constraint name from the underlying SQL exception using: ((JDBCException)exception).getSQLException()

As epbernard suggests, make sure your DAO is not eating exceptions.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 12:03 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
DAO is not eating exceptions but another method are. Sorry, my mistake.

Thanks a lot.


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