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 769539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:21:10+00:00 2026-05-14T18:21:10+00:00

I’m using JPA 2.0/Hibernate validation to validate my models. I now have a situation

  • 0

I’m using JPA 2.0/Hibernate validation to validate my models. I now have a situation where the combination of two fields has to be validated:

public class MyModel {
    public Integer getValue1() {
        //...
    }
    public String getValue2() {
        //...
    }
}

The model is invalid if both getValue1() and getValue2() are null and valid otherwise.

How can I perform this kind of validation with JPA 2.0/Hibernate? With a simple @NotNull annotation both getters must be non-null to pass validation.

  • 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-14T18:21:11+00:00Added an answer on May 14, 2026 at 6:21 pm

    For multiple properties validation, you should use class-level constraints. From
    Bean Validation Sneak Peek part II: custom constraints:

    Class-level constraints

    Some of you have expressed concerns
    about the ability to apply a
    constraint spanning multiple
    properties, or to express constraint
    which depend on several properties.
    The classical example is address
    validation. Addresses have intricate
    rules:

    • a street name is somewhat standard and must certainly have a length limit
    • the zip code structure entirely depends on the country
    • the city can often be correlated to a zipcode and some error checking can
      be done (provided that a validation
      service is accessible)
    • because of these interdependencies a simple property level constraint does
      to fit the bill

    The solution offered by the Bean
    Validation specification is two-fold:

    • it offers the ability to force a set of constraints to be applied before an
      other set of constraints through the
      use of groups and group sequences.
      This subject will be covered in the
      next blog entry
    • it allows to define class level constraints

    Class level constraints are regular
    constraints (annotation /
    implementation duo) which apply on a
    class rather than a property. Said
    differently, class-level constraints
    receive the object instance (rather
    than the property value) in isValid.

    @AddressAnnotation 
    public class Address {
        @NotNull @Max(50) private String street1;
        @Max(50) private String street2;
        @Max(10) @NotNull private String zipCode;
        @Max(20) @NotNull String city;
        @NotNull private Country country;
        
        ...
    }
    
    @Constraint(validatedBy = MultiCountryAddressValidator.class)
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AddressAnnotation {
        String message() default "{error.address}";
        Class<?>[] groups() default { };
        Class<? extends Payload>[] payload() default { };
    }
    
    public class MultiCountryAddressValidator implements ConstraintValidator<AddressAnnotation, Address> {
        public void initialize(AddressAnnotation constraintAnnotation) {
        // initialize the zipcode/city/country correlation service
        }
    
        /**
         * Validate zipcode and city depending on the country
         */
        public boolean isValid(Address object, ConstraintValidatorContext context) {
            if (!(object instanceof Address)) {
                throw new IllegalArgumentException("@AddressAnnotation only applies to Address objects");
            }
            Address address = (Address) object;
            Country country = address.getCountry();
            if (country.getISO2() == "FR") {
                // check address.getZipCode() structure for France (5 numbers)
                // check zipcode and city correlation (calling an external service?)
                return isValid;
            } else if (country.getISO2() == "GR") {
                // check address.getZipCode() structure for Greece
                // no zipcode / city correlation available at the moment
                return isValid;
            }
            // ...
        }
    }
    

    The advanced address validation rules
    have been left out of the address
    object and implemented by
    MultiCountryAddressValidator. By
    accessing the object instance, class
    level constraints have a lot of
    flexibility and can validate multiple
    correlated properties. Note that
    ordering is left out of the equation
    here, we will come back to it in the
    next post.

    The expert group has discussed various
    multiple properties support
    approaches: we think the class level
    constraint approach provides both
    enough simplicity and flexibility
    compared to other property level
    approaches involving dependencies.
    Your feedback is welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.