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

Hardy Ferentschik (JIRA) noreply at atlassian.com
Fri May 14 12:55:24 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HV-230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=37135#action_37135 ] 

Hardy Ferentschik commented on HV-230:
--------------------------------------

Decided to go for the wiki option for now - http://community.jboss.org/wiki/AccessingtheHibernateSessionwithinaConstraintValidator

> Database Connective or @Unique
> ------------------------------
>
>                 Key: HV-230
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HV-230
>             Project: Hibernate Validator
>          Issue Type: New Feature
>          Components: validators
>    Affects Versions: 3.1.0.GA
>            Reporter: Ken Egervari
>            Assignee: Hardy Ferentschik
>            Priority: Critical
>             Fix For: 4.1.0
>
>         Attachments: HV-230.patch
>
>
> 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