Have you ever wished , if your favorite framework provides some kind of functionality that validates unique constraints against your Database tables. Not until now , most of us were catching exceptions and look for Sql error code for unique constraint or doing some kind of work around to handle unique constraint issues. Which is not so clean and nice looking. Fortunately, with JBoss Seam and Hibernate validator we can handle unique constraint problem in a elegant way. And did i say that it will be Generic too. Yes it will work for all of your tables. Lets see how Jboss Seam will handle Unique constraint problem and returns a nice looking message. Suppose we have a table called Users with userName and some other property. Do i need to say userName should be unique. | ||||||||
| ||||||||
Before looking at User domain class , lets create an annotation called @Unique | ||||||||
@Unique is a simple annotation with 2 properties entityName - your mapped class fieldName - property that should be unique for this entity. And UniqueValidator class
Now we just have to apply this @Unique annotation to User class
Add <s:validateall> in your view
That's it!! Now Seam will auto magically show you "already exist" message as you will tab out of user name text box. Thats Seam Magic. |
Sunday, March 15, 2009
Hibernate Unique validator with Seam
Labels:
Hibernate Validator,
icefaces,
JBoss Seam,
Unique constraint,
validator
Subscribe to:
Posts (Atom)