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

  • SEARCH
  • Home
  • 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 8537011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:47:51+00:00 2026-06-11T10:47:51+00:00

I am using Hibernate validator like @NotEmpty to see if a specific property in

  • 0

I am using Hibernate validator like @NotEmpty to see if a specific property in a class is empty or not. The class is as as shown:

@Entity
@Table(name="emergency_messages")
public class EmergencyMessages implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", nullable=false)
private Integer id;

@NotEmpty(message="Home message cannot be empty")
@Column(name="home_page_message")
private String homePageMessage;

@Range(min=0, max=1, message="Please select one of the display announcement value")
@Column(name="messages_enabled")
private Integer messagesEnabled;
}

So far so good. Whenever the property “homePageMessage” is empty I can see that the correct error message in the form in the browser.

Now the situation has changed. The new requirement is that the property “homePageMessage” can be empty only if the other property “messagesEnabled” is set to 1. If it is set to 0 then there should be no empty check done for “homePageMessage”. In simple words the validation of “homePageMessage” should now be dependent on the “messagesEnabled” value.

My question: Is this possible to do with annotations? If not, then I will have to dismantle my hibernate validator mechanism and create my own validation class.

  • 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-06-11T10:47:52+00:00Added an answer on June 11, 2026 at 10:47 am

    Following is the code that I came up with (after suggestions from Ajinkya and Alex):

    Customized Annotation:

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    import javax.validation.Constraint;
    import javax.validation.Payload;
    
    @Target({ElementType.TYPE})  
    @Retention(RetentionPolicy.RUNTIME)  
    @Documented  
    @Constraint(validatedBy=HomePageEmptyMessageValidator.class)
    public @interface HomePageEmptyMessage {
    
    String message() default "";
    
     Class<?>[] groups() default {};
    
     Class<? extends Payload>[] payload() default {};
    }
    

    Customized Validator:

    import javax.validation.ConstraintValidator;
    import javax.validation.ConstraintValidatorContext;
    
    public class HomePageEmptyMessageValidator implements ConstraintValidator<HomePageEmptyMessage, EmergencyMessages> {
    
    @Override
    public void initialize(HomePageEmptyMessage homePageEmptyMessage) {
    }
    
    @Override
    public boolean isValid(EmergencyMessages emergencyMessages, ConstraintValidatorContext context) {
    
        if (emergencyMessages == null) {
            return false;
        }
    
        Integer messageEnabled = emergencyMessages.getMessagesEnabled();
        if (messageEnabled != null) {
            if (messageEnabled == 1) {
                String homePageMessage =  emergencyMessages.getHomePageMessage();
                if (Util.isNullOrEmpty(homePageMessage)) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return true;
            }
        }
    
        return false;
    }
    
    }
    

    Usage of customized annotation in the code:

    @Entity
    @Table(name="emergency_messages")
    @HomePageEmptyMessage(message="Home page annoucement cannot be empty if the Display  Announcement is set to Yes")
    public class EmergencyMessages implements Serializable {
    
    private static final long serialVersionUID = -7870767517772161300L; 
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id", nullable=false)
    private Integer id; 
    
    @Column(name="home_page_message")
    private String homePageMessage;
    
    @Range(min=0, max=1, message="Please select one of the display announcement value")
    @Column(name="messages_enabled")
    private Integer messagesEnabled;
    }
    

    I hope it helps someone.

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

Sidebar

Related Questions

I'm looking into using JSR-303 with hibernate validator. We would like to be able
I'm using Hibernate Validator. I have a Class level annotation. It compares three properties
I'm using Hibernate validator 4.1 to validate my Entity. I have a dashboard that
Using hibernate-validator-4.2.0.Final.jar and the ClassValidator class is nowhere to be found inside of it.
I am using hibernate validator to validate a form. However the validator does not
I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand
I'm trying to integrate bean validation using hibernate validator to my web application :
I'm working on a Spring MVC project in which I'm using Hibernate Validator to
Using hibernate, how can I persist a class with a List<String> field? Consider the
I have problem with using Hibernate Validator with GWT 2.4. When module is starting

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.