Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 583675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:49:03+00:00 2026-05-13T14:49:03+00:00

Is there an implementation of (or third-party implementation for) cross field validation in Hibernate

  • 0

Is there an implementation of (or third-party implementation for) cross field validation in Hibernate Validator 4.x? If not, what is the cleanest way to implement a cross field validator?

As an example, how can you use the API to validate two bean properties are equal (such as validating a password field matches the password verify field).

In annotations, I’d expect something like:

public class MyBean {
  @Size(min=6, max=50)
  private String pass;

  @Equals(property="pass")
  private String passVerify;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:49:04+00:00Added an answer on May 13, 2026 at 2:49 pm

    Each field constraint should be handled by a distinct validator annotation, or in other words it’s not suggested practice to have one field’s validation annotation checking against other fields; cross-field validation should be done at the class level. Additionally, the JSR-303 Section 2.2 preferred way to express multiple validations of the same type is via a list of annotations. This allows the error message to be specified per match.

    For example, validating a common form:

    @FieldMatch.List({
            @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"),
            @FieldMatch(first = "email", second = "confirmEmail", message = "The email fields must match")
    })
    public class UserRegistrationForm  {
        @NotNull
        @Size(min=8, max=25)
        private String password;
    
        @NotNull
        @Size(min=8, max=25)
        private String confirmPassword;
    
        @NotNull
        @Email
        private String email;
    
        @NotNull
        @Email
        private String confirmEmail;
    }
    

    The Annotation:

    package constraints;
    
    import constraints.impl.FieldMatchValidator;
    
    import javax.validation.Constraint;
    import javax.validation.Payload;
    import java.lang.annotation.Documented;
    import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
    import static java.lang.annotation.ElementType.TYPE;
    import java.lang.annotation.Retention;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
    import java.lang.annotation.Target;
    
    /**
     * Validation annotation to validate that 2 fields have the same value.
     * An array of fields and their matching confirmation fields can be supplied.
     *
     * Example, compare 1 pair of fields:
     * @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match")
     * 
     * Example, compare more than 1 pair of fields:
     * @FieldMatch.List({
     *   @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"),
     *   @FieldMatch(first = "email", second = "confirmEmail", message = "The email fields must match")})
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Constraint(validatedBy = FieldMatchValidator.class)
    @Documented
    public @interface FieldMatch
    {
        String message() default "{constraints.fieldmatch}";
    
        Class<?>[] groups() default {};
    
        Class<? extends Payload>[] payload() default {};
    
        /**
         * @return The first field
         */
        String first();
    
        /**
         * @return The second field
         */
        String second();
    
        /**
         * Defines several <code>@FieldMatch</code> annotations on the same element
         *
         * @see FieldMatch
         */
        @Target({TYPE, ANNOTATION_TYPE})
        @Retention(RUNTIME)
        @Documented
                @interface List
        {
            FieldMatch[] value();
        }
    }
    

    The Validator:

    package constraints.impl;
    
    import constraints.FieldMatch;
    import org.apache.commons.beanutils.BeanUtils;
    
    import javax.validation.ConstraintValidator;
    import javax.validation.ConstraintValidatorContext;
    
    public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
    {
        private String firstFieldName;
        private String secondFieldName;
    
        @Override
        public void initialize(final FieldMatch constraintAnnotation)
        {
            firstFieldName = constraintAnnotation.first();
            secondFieldName = constraintAnnotation.second();
        }
    
        @Override
        public boolean isValid(final Object value, final ConstraintValidatorContext context)
        {
            try
            {
                final Object firstObj = BeanUtils.getProperty(value, firstFieldName);
                final Object secondObj = BeanUtils.getProperty(value, secondFieldName);
    
                return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
            }
            catch (final Exception ignore)
            {
                // ignore
            }
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something like a two sided list in Java? Maybe a third party
Is there any implementation of regex that allow to replace group in regex with
This is just to satisfy my own curiosity. Is there an implementation of this:
Is there an Iterator implementation that merges multiple iterators? class MergedIterator<T> implements Iterator<T> {
Is there an openID implementation in Java? I would like to use this in
Does anyone know if there is an implementation of javax.jms.QueueConnectionFactory for WebSphere MQ and
Is there an open source implementation of Google AdWords, or is the technology protected
Is there a good equivalent implementation of strptime() available for Windows? Unfortunately, this POSIX
I heard there is a light implementation of boost where its only smart pointers
I am interested if there is a port for the server implementation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.