-->
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: About Hibernate exceptions
PostPosted: Sun Aug 07, 2005 3:06 pm 
Newbie

Joined: Tue Jan 25, 2005 5:05 pm
Posts: 17
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

How to handle HibernateException's correctly? For ex. I wanna show to user message "Object already exists" when he save Object to database, but all I have - HibernateException and I can't determine the reason.
Is that unique key violation or some column's constraint violation. How to determine that?

For example: I have a business method

// alsow throws unchecked SystemException which mean some database failure or external systems failure.
createUserAccount(UserAccount a) throws UserExistsException, ValidationException {
session.save(a) // HibernateException can be thrown in case of Account already exisit in database or Account name length < 1 char. Alsow in all cases HibernateException throws.
}

But I wanna throw specified exception for specifid case. How I should make it?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 4:00 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
In your method createUserAccount you could manually check (with a query), if the user already exists in the database. If he does, you can throw your own exception.

Something like this:
Code:
Query query = session.createQuery("SELECT COUNT(*) FROM User AS u WHERE u.username = :username");
query.setString("username", username);
int numberOfUsernames = ((Integer)query.uniqueResult()).intValue();
if (numberOfUsernames > 0) {
   throw new...
}


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 4:24 pm 
Newbie

Joined: Tue Jan 25, 2005 5:05 pm
Posts: 17
sven wrote:
In your method createUserAccount you could manually check (with a query), if the user already exists in the database. If he does, you can throw your own exception.

Something like this:
Code:
Query query = session.createQuery("SELECT COUNT(*) FROM User AS u WHERE u.username = :username");
query.setString("username", username);
int numberOfUsernames = ((Integer)query.uniqueResult()).intValue();
if (numberOfUsernames > 0) {
   throw new...
}


Best regards
Sven


First. Ok. I am doing that now. But I should execute queries for each unique column (Yes it's possible to make using OR statement in HQL, I wanna to provide message which column not unique). Is it normal way?

Second. What about length constraints in relation model? How to handle them? I see only one way - validate data on client side and any relations errors consider as UNRESOLVABLE RuntimeExceptions. It this good?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 4:52 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
1.
Are there so many unique columns for your class?

This way of querying might also be helpful:
http://www.hibernate.org/hib_docs/v3/re ... a-examples


2.
Validating data on the client side is a good idea.

I don't know the type of your application. If it's a web application based on Struts, you could easily implements validation using Jakarta Commons Validator (http://jakarta.apache.org/commons/validator/) in your beans.

You can also implement a so-called check constraint on database level. But this requires your database to support this type of constraint (e.g. Oracle).
Code:
ALTER TABLE your_table ADD CONSTRAINT constraint_name CHECK (column_name > 5);

Keep in mind that AFAIK there won't be a database error, if you try to insert a 100 character String into a 10 character database column - it will just be truncated.

You can also have a look at the Hibernate Validator (http://www.hibernate.org/hib_docs/annot ... #validator), but I don't know which version of Hibernate you're using. (It's a new feature in combination with Hibernate Annotations.)

Best regards
Sven


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.