]
Hardy Ferentschik updated HV-230:
---------------------------------
Fix Version/s: 4.1.0
Database Connective or @Unique
------------------------------
Key: HV-230
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HV-230
Project: Hibernate Validator
Issue Type: New Feature
Affects Versions: 3.1.0.GA
Reporter: Ken Egervari
Priority: Critical
Fix For: 4.1.0
I find a common validation use case is checking to see if a field is unique to all those
in the table, such as emails, isbns, usernames, keywords, etc. these fields may not be the
primary key, but still need to be unique.
It would be fantastic if hibernate validator implemented this.
This is a common problem though because the domain class will need to have access the
database. I dunno if there's an easy way to wire in a copy of sessionFactory to make
this easy to write.
Perhaps something like this:
@Query( "select user from User user where user.emailAddress = :this.emailAddress and
user.id != :this.id", message = "That Email is already being used by another
user in the system" )
private String emailAddress;
Basically the idea is that if @Query returns no results, then the validation constraint
is good, and if returns more than 1 result, then it fails. Kind of like
simpleJdbcTemplate.queryForMap() does within the Spring testing framework. That method
causes the test to fail if no row is returned, because it expects 1 result.
If it were possible to just say
@Unique
private String emailAddress;
That would be extremely concise and would save people a ton of time.
Of course, this requires some interoperability with Hibernate... but I think that's a
good thing, no?
Thanks for taking this into consideration. I'd appreciate an email letting me know
how to do this in a clean way in 3.1.0 GA as it is as well if you would. Thank you.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: