Skip to content
Advertisement

Hibernate Check Annotation

I have a table with three fields, say a, b, c. I would like to add a constraint ensuring that if a is not null, then also b and c are not null. I have done that using following SQL

Is there a way to achieve same effect using hibernate annotation @Check?

I can’t find a helpful example with that annotation, do developers tend not to use it at all?

Advertisement

Answer

Yes it is possible if @Check is used at class level like this:

(Note that I rewrote your condition using @jarlh comment.). The constraints clause of @Check annotation needs to refer to the name attribute of @Column (it must be pure SQL).

@Check annotation needs to be at class level because of a Hibernate bug.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement