"Dynamic" java validation framework?

AFAIK JSR-303 is a standard bean validation system.

I don’t know if he could do such checks (I think not):

  • If the object has the flag set, you cannot change the object
  • you cannot change the start date property after passing the date
  • you cannot reduce some integer properties in a bean

So, how can I handle checks that depend on the previous state of the object?

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

thank


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

+3
source share
7 answers

I do not think this can be done using JSR 303 validation (or any other validation framework I used). Validation is usually inactive - you pass it an instance of the object, and your validation environment checks things to make sure that the current values ​​of your object are valid. There is no real knowledge of the previous states of the object.

You can do this - simply not with validation. You can use the property of a restricted property , or you can do this work using a proxy template or AOP.

+2
source

, , ( ), , . (idDeleted, createdDate ..) . , , , , , .

, , , , , , . . Hibernate , , INSERT UPDATE .

, .

+1

, ?

100%, , , - , " " " " (), . , , , .

0

, , , , , . , , . .

, makeACopy(), .

, Clonable , , . makeACopy() , .

0

. , JSR-303 , "".

...

. ...

,

, . - "deleted". true, , .

,

. , ( ) .

bean

, , - ( ).

, AOP - , . .

, , JPA. Spring AOP , Spring .

0

, . , .

:

public void setCounter(int newCounter) {
    if (newCounter < this.counter) {
       throw new IllegalOperationException("Cannot decrease the counter");
    } else {
       this.counter = newCounter;
    }
}
0

Instead, you can watch OVal . We check all this time. This is usually done using SimpleCheck, where you get the object and value and can do all kinds of cross-validation.

0
source

Source: https://habr.com/ru/post/1746143/


All Articles