java - jOOQ: Allowed-Character constraints? -
i considering moving hibernate jooq can't find e.g. how have pattern-constraints on string
in hibernate:
@notempty(message = "firstname cannot empty") @pattern(regexp = "^[a-za-z0-9_]*$", message = "first name can contain characters.") private string firstname;
how in jooq?
the "jooq way"
the "jooq way" such validation create either:
- a
check
constraint in database. - a trigger in database.
- a domain in database.
after all, if want ensure data integrity, database such constraints , integrity checks belong (possibly in addition functionally equivalent client-side validation). imagine batch job, perl script, or jdbc statement bypasses jsr-303 validation. you'll find corrupt data in no time.
if want implement client-side validation, can still use jsr-303 on dtos, interact ui, instance. have perform validation before passing data jooq storage (as artbristol explained).
using converter
you could, however, use own custom type declaring converter
on individual columns , registering such converter
source code generator.
essentially, converter
is:
public interface converter<t, u> extends serializable { u from(t databaseobject); t to(u userobject); class<t> fromtype(); class<u> totype(); }
in case, implement annotations such:
public class notemptyalphanumericvalidator implements converter<string, string> { // validation public string to(string userobject) { assertnotempty(userobject); assertmatches(userobject, "^[a-za-z0-9_]*$"); return userobject; } // boilerplate public string from(string databaseobject) { return databaseobject; } public class<string> fromtype() { return string.class; } public class<string> totype() { return string.class; } }
note more of workaround, converter
hasn't been designed use-case, if can implement it.
using formal client-side validation
there's pending feature request #4543 add more support client-side validation. of jooq 3.7, not yet implemented.
Comments
Post a Comment