Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Dynamic" java validation framework?

AFAIK JSR-303 is the standard bean validation system.

I don't know whether it could do validations like this (I guess no):

  • if an object has a deleted flag set, you cannot modify the object
  • you cannot change the start date property, after the date is passed
  • you cannot decrease some integer properties in the bean

So how can I handle validations, which depend on the previous state of an object?

I would like to solve problems like that in hibernate3.5 - spring3 - JPA2 environment.

Thanks


My solution was to mess with hibernate, reload the object to see the old state (after evicting the new object). This time I need some smarter solution...

like image 742
pihentagy Avatar asked Aug 29 '26 06:08

pihentagy


2 Answers

I don't think this can be done using JSR 303 validation (or any other validation framework I've used). Validation is usually stateless - you pass it an instance of an object, and your validation framework tests things to make sure the current values of your object are valid. There's no real knowledge of previous states of the object.

You can do this - just not with validation. You could use a constrained property, or you could make this work using the proxy pattern or AOP.

like image 179
Nate Avatar answered Aug 30 '26 18:08

Nate


It sounds like the fields which you want to validate (with regards to previous state) are all metadata about the records as opposed to real data. All of these fields (idDeleted, createdDate, etc.) are better left out of your domain layer and therefor do not require validation. I would put the logic for determining & setting these values in you data-access layer so that the systems using your repository interfaces do not need to know or care about getting them right.

If my assumption about these fields being meta-data is not correct and you have user-entered data which validation depends on previous state, then I do not think that an extra lookup for the previous values is absurd and should not be out of the question. It makes sense in your case. Hibernate itself does a lookup under then hood to determine whether to INSERT or UPDATE when using it's save function.

Hope you find a reasonable solution.

like image 28
Jesse Webb Avatar answered Aug 30 '26 18:08

Jesse Webb