[
http://opensource.atlassian.com/projects/hibernate/browse/HV-230?page=com...
]
Hardy Ferentschik updated HV-230:
---------------------------------
Attachment: HV-230.patch
Attaching a patch which shows how a {{@Unique}} constraint could be implemented using the
"open temporary session on top of current session's connection" approach.
Included in the patch is a {{SessionAwareConstraintValidatorFactory}}. We could add a
check into {{BeanValidationEventListener.validate}} to see which type the current factory
is and if it is the {{SessionAwareConstraintValidatorFactory}} inject the
{{SessionFactory}} automatically.
The reason I am not committing this patch yet is that introducing the {{@Unique}} creates
some problems. Most importantly there might be [phantom
reads|http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Repea...].
If we add {{@Unique}} as custom constraint we need to make sure that we add appropriate
warning notes to the documentation.
The alternative is to provide the sample code on the Validator
[
wiki|http://community.jboss.org/en/hibernate/validator]
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
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....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira