JAVA - Unique constraint with JPA and Bean Validation

I'd like to have a @Unique constraint with Bean Validation, but that is not provided by the standard. If I would use JPA's @UniqueConstraint I wouldn't have a unique validation and error reporting mechanism.

Is there a way to define @Unique as a Bean Validation constraint and combine it with JPA, such that JPA creates a column with an unique constraint and checks wheter a value is unique or not?

This question and answers originated from www.stackoverflow.com
Question by (8/16/2010 4:53:07 PM)

Answer

Unless you acquire a lock on a whole table, it is basically not possible to check for unicity using a SQL query (any concurrent transaction could modify data after a manual check but before the commit of the ongoing transaction). In other words, it isn't possible to implement a valid unique verification at the Java level and thus to provide a validation implementation. The only reliable way to check for unicity is while committing the transaction.

The BV spec summarizes it like this:

Appendix D. Java Persistence 2.0 integration

Question: should we add @Unique that would map to @Column(unique=true)?

@Unique cannot be tested at the Java level reliably but could generate a database unique constraint generation. @Unique is not part of the BV spec today.

So while I agree that it would be nice to have unique (and non null) constraint violations wrapped in a Bean Validation exception, this is currently not the case.

References

Answer by

Find More Answers
Related Topics  java  validation  jpa  unique  bean-validation
Related Questions
  • JPA unique constraint validation

    How to validate Multiple column unique constraint in JPA entity. Is there any Hibernate validator for this purpose. I have added @UniqueConstraint annotation. But it is not doing any validation.And …
  • JSR-303 and JPA overlapping

    Aloha hey! I already use JPA annotations, now I'd like to use JSR-303 annotations to validate attributes as well. My questions is, how well do they interact with each other? If I use @Basic(o…
  • JPA validation strategy

    NetBeans let me choose between three values for the JPA validation strategy: Auto, Callback and None. What does "Auto" mean? Does "Callback" mean the use of @PrePersist, @PreUpdate, and @PreRemove? …
  • Bean Validation Through JPA Relationships

    I would like to use Bean Validation to annotate constraints in my entities , now the question is, will the relationships on the entities be validated as well?. For example suppose I have the followi…
  • Should I use Bean Validation with EJB 3.1, JSF2.0 and JPA?

    I have a JSF 2.0 application that I would like to start adding validators for. A basic overview of the architecture of the app is as follows. I have Managed Backing Beans that contain instances o…
  • Hibernate validation: unique constraint

    I'm fairly new to Hibernate, and I'm learning as i go. I really enjoy all the tools hibernate has to offer, including the validation tools. Validation is great because I can generate human readible …
  • Lost transaction with JPA unique constraint?

    I have a field with an unique constraint: @Column(unique=true) private String uriTitle; When I try to save two entities with the same value, I get an exception - but another exception than I …
  • JPA Entitites's Bean Validation messages as Resource Bundle?

    Does Bean Validation with JPA entity has the integrated feature of making use of the resourcebundle just like the BV in JSF ? And if it can, can i parameterize the resource bundle, so i can have …
  • How to explicite assign a Bean Validation (JSR303) constraint to an bean property?

    I use Bean Validation for Spring MVC. And Spring binds the validation failures to the bean properties where the failing annotation is defined, as well as class based constraints to the bean itself. …
  • Best way to prevent unique constraint violations with JPA

    I have an Keyword and a KeywordType as entities. There are lots of keywords of few types. When trying to persist the second keyword of a type, the unique constraint is violated and the transaction …