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.  [ 7 posts ] 
Author Message
 Post subject: Know the unique fields who produced the exception
PostPosted: Tue Aug 31, 2004 1:27 pm 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Hi all:

I'm inserting an object with (let's say) 3 unique fields
- username
- email
- project

Is there any simple way to buid a specific exception who says:

This email exists in the system, please type a new one.

The exception provided right now is
Code:
java.sql.SQLException: ERROR:  Cannot insert a duplicate key into unique index optuser_username_key


Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The way I've done this in the past is to use SQLException interpreters. Quite slick actually, if I say so myself :)

The approach I used was to:
1) explicitly name my constraints in the DB;
2) use a SQLException interpreter that uses sql-state or error-code to convert the exception to a "typed hierarchy" (there are existing libraries that do this like Spring);
3) based on the "type" of exception, parse the violated constraint; the constraint name can then be used to read into a ResourceBundle, etc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 1:51 pm 
Beginner
Beginner

Joined: Tue Feb 10, 2004 2:30 pm
Posts: 25
The exception strings are usually DB-type centric too, so if you're writing portable code you have to allow for that.

The JDBC exception from Hibernate is also "nice" in that it multiple exceptions put into an array - this makes parsing that much easier too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 11:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
believe me, the exception strings being db-centric are the least of your worries here as which of the two fields to read and what the values actually mean are almost completely db dependant. At the point in processing that you know the type of exception based on the sql-state or error-code you more than likely know how to parse the exception message because in order to parse the sql-state or error-code you more than likely know the type of DB you are talking to.


Top
 Profile  
 
 Post subject: What i'm going to do
PostPosted: Wed Sep 01, 2004 11:16 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Well, so ...
there is no built-in method to do the trick. It's a pity.

The process involved here is the register of a new user, so there is not a performance need involved.

I will do this:

Code:
try{
save(object)
}
catch(Exception e)
{
if (simpleParseUniqueField(e))
{
String field = getUniqueProblemField(object)
// a MAX of N-1 queries here, where N is the number of unique fields in object
exposeError(field)
}


Top
 Profile  
 
 Post subject: What i'm going to do
PostPosted: Wed Sep 01, 2004 11:18 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Well, so ...
there is no built-in method to do the trick. It's a pity.

The process involved here is the register of a new user, so there is not a performance need involved.

I will do this:

Code:
try{
save(object)
}
catch(Exception e)
{
if (simpleParseUniqueField(e))
{
String field = getUniqueProblemField(object)
// a MAX of N-1 queries here, where N is the number of unique fields in object
exposeError(field)
}


Top
 Profile  
 
 Post subject: What i'm going to do
PostPosted: Wed Sep 01, 2004 11:22 am 
Newbie

Joined: Tue Jul 20, 2004 7:03 am
Posts: 19
Location: Spain
Well, so ...
there is no built-in method to do the trick. It's a pity.

The process involved here is the register of a new user, so there is not a performance need involved.

I will do this:

Code:
try{
save(object)
}
catch(Exception e)
{
if (simpleParseUniqueField(e))
{
String field = getUniqueProblemField(object)
// a MAX of N-1 queries here, where N is the number of unique fields in object
exposeError(field)
}


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