[hibernate-issues] [Hibernate-JIRA] Created: (HV-230) Database Connective or @Unique

Ken Egervari (JIRA) noreply at atlassian.com
Sat Sep 19 23:20:50 EDT 2009


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


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: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list