Dhana
Dhana

Reputation: 913

Bean Validation - JSR-303 DuplicateItemCheck for a list in java

Is there any standard bean validation annotation to check duplicate item in a collection in java. or Anyone implemented custom validation to check duplicate in a list using bean validation.

for example



    public class MySecurityRequest{
        private Date dob;

        @DuplicateNotAllowed
        private List securityQuestions;
    }

    public class SecurityQuestion{
      private String question;
      private String answer;
    }

Here i need to make sure that, questions are not repeated in the list. Are one faced this similar issue?

Upvotes: 1

Views: 2002

Answers (2)

Guillaume Smet
Guillaume Smet

Reputation: 10519

Ah ah funny you ask. We just added the @UniqueElements annotation to Hibernate Validator (the PR was merged today - https://github.com/hibernate/hibernate-validator/pull/879).

It will be part of 6.0.5.Final that we plan to release tomorrow.

Note that it is not a standard constraint (i.e. it's not in Bean Validation) but it will be supported by HV out of the box.

You should be able to use HV 6 with Spring: it should be compatible. Just be careful about your dependencies (you can't have both versions in your dependencies - the group ids of HV 5 and 6 are different - and also be careful about the javax.el dependency - see https://github.com/hibernate/hibernate-validator#using-hibernate-validator for the details).

Upvotes: 7

Tharun
Tharun

Reputation: 311

Use a Set. And override and implement hashcode() and equals() methods on your SecurityQuestion class to help the Set identify duplicates.

Upvotes: 0

Related Questions