-
1. Re: How to validate for uniqueness constraint consisting of multiple columns?
jmiettin May 10, 2010 5:11 PM (in response to jmiettin)The @Column is supposed to be like the following:
public class Bar { . . . @Column(unique = true) public String getName() { return name; } }
-
2. Re: How to validate for uniqueness constraint consisting of multiple columns?
raphaufrj May 11, 2010 12:41 AM (in response to jmiettin)Hello Jarkko...
Your question is about Bean Validation and not Jboss Seam. Although, you have to create your own Annotation Validation in this case. It sounds like :
public class Bar { @UniquenessBarName @Column(unique=true) public String getName() { return name; } }
To create @UniquenessBarName above, you can follow the tutorial : 'http://java.dzone.com/articles/using-hibernate-validator'
Regards,
-
3. Re: How to validate for uniqueness constraint consisting of multiple columns?
jmiettin May 11, 2010 9:10 AM (in response to jmiettin)Why I asked the question here was that I was led astray by the sentence
Seam provides support for model-based constraints defined using Hibernate Validator.
Now that I think of it, Hibernate Validator indeed does not work with the database (@NotNull annotation does not the JPA-generated schema to have 'not null' constraint) but instead just verifies the values at persisting step.As the constraints I want to enforce span multiple values (e.g. combination of getFoo() and getName() must be unique), my only possibility seems to be to persist the Bar earlier in the JSF lifecycle and if it fails, catch the exception and report the error.
-
4. Re: How to validate for uniqueness constraint consisting of multiple columns?
raphaufrj May 11, 2010 8:18 PM (in response to jmiettin)Jarkko,
I understand your necessity right now... I think that hibernate validator 4 has a feature to validate top level class. This link can help you: http://musingsofaprogrammingaddict.blogspot.com/2010/02/generic-class-level-constraint-for-bean.html
Regards,
-
5. Re: How to validate for uniqueness constraint consisting of multiple columns?
thokuest May 11, 2010 8:19 PM (in response to jmiettin)For anybody who's interested, there is a nice article on the problem on how to apply a constraint to elements of a collection.
Jarkko Miettinen wrote on May 11, 2010 09:10:
As the constraints I want to enforce span multiple values (e.g. combination of getFoo() and getName() must be unique), my only possibility seems to be to persist the Bar earlier in the JSF lifecycle and if it fails, catch the exception and report the error.Oh no, just a thought by myself:
public class Foo { @UniqueBar private Set<Bar> bars; }
-
6. Re: How to validate for uniqueness constraint consisting of multiple columns?
hbender May 12, 2010 9:06 AM (in response to jmiettin)Another possibility is to use an embedded class which represents the composed primary key. Use the annotations @Embeddable and @EmbeddedId
Heri -
7. Re: How to validate for uniqueness constraint consisting of multiple columns?
jmiettin May 12, 2010 3:39 PM (in response to jmiettin)Thanks for the suggestions! The biggest problem here was that I was a bit confused about what I wanted. The constraints I have exist at DB level and I first wanted to get the validation to upper level by using JSF validation without repeatin myself. Doing that would have required me to handle cases where input is valid at first but not when it is persisted.
I now persist the new Bars in validator (ugh) and in case the persisting fails, I rollback the changes and fail the validation.
Thanks for the links on validation, they will come handy in other cases!Btw, am I the only one who is totally fed up with Wordpress themes that have whole 460 px available for text and thus cause a lot of wrapping on code samples?