[hibernate-issues] [Hibernate-JIRA] Commented: (HV-230) Database Connective or @Unique
Chris Wilson (JIRA)
noreply at atlassian.com
Fri Nov 26 12:06:13 EST 2010
[ http://opensource.atlassian.com/projects/hibernate/browse/HV-230?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39218#action_39218 ]
Chris Wilson commented on HV-230:
---------------------------------
If you came to this page looking for an implementation of @Unique, you might like to check out
http://blog.aptivate.org/2010/11/26/hibernate-ejb-and-the-unique-constraint/
and
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: 4.0.0.Alpha1
> Reporter: Ken Egervari
> Assignee: Hardy Ferentschik
> Priority: Critical
> Fix For: 4.1.0.Beta2
>
> 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