-->
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: Best way to prevent a duplicate reccord in a unique field
PostPosted: Thu Apr 06, 2006 7:29 pm 
Newbie

Joined: Tue Apr 04, 2006 1:00 pm
Posts: 8
Hi!

I'm new to Hibernate and wonder if any one have a suggestive way to deal with duplicated reccords for databasefields with unique properties. e.g a username field.

I'm writing a webapplicatin that (among other things :) simply inserts new Users. Now, what would be the best practice to handle the exception that occures if someone tries to insert a duplicate (username -Hibernate throws a ConstraintViolationException)?

I've been thinking that catching the exception could be a good way, but I allso think i've read something about that being a problem - (something about rollback)??
The other solution I've thought of is to do a simple hql check to see if the user (username) exist at first, and then either insert the new user or not.

Any good ideas?

Thanks in advance
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 6:54 am 
Newbie

Joined: Tue Apr 04, 2006 1:00 pm
Posts: 8
hmm.. maybe this was a really stupid question?
Anyway - are anyone able to explain to me what the big thing about rollback issue is?

Can I at all cath exceptions that Hibernate throws (and execute my own error message as String to a webpage) or will this some how affect Hibernates handling of the objects?

Thanks for any replay :)

Robert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 8:35 am 
Newbie

Joined: Mon Mar 20, 2006 2:30 pm
Posts: 1
Location: Germany
I'd prefer catching the ConstraintViolationException and displaying information like "username x beeing already assigned, choose other" instead of explicit querying this condition by yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 12, 2006 7:49 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 2:33 pm
Posts: 30
Location: CA, USA
The problem with catching the constraint violation is determining when it will be thrown. If you are using the Session In View and GenericDAO patterns where your transaction is being handled outside your DAO and the session flushing is set to auto, the exception will usually be thrown when you commit your transaction, not when you issue the save() or saveOrUpdate().

So if you want to catch the exception, make sure you call session.flush() so you know the DML statement has been run against the database.


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.