@NotNull default in JPA / Hibernate

I have a MySql database with multiple tables. Every field in every table is not NULL. So I am forced to use annotation @NotNull

for every field in all Java classes that are tagged @Entity

. Should I actually do this or is there a way to tell JPA / Hibernate to handle each NotNullable field by default?

edit: I know too @Column(name="something", nullable=false)

. But nevertheless, this does not solve any problems - you need to writenullable=false

+3


source to share


1 answer


There is no such possibility. Not Nullable

Constraint is not what you always expect from a field, although it is used quite often. This is useful when you can look at the definition of an attribute and tell everything without having to go through some high-level settings like "every field must be @NotNull

".

It would be rather difficult to see such an entity definition with this parameter hidden elsewhere.



And one more thing. The annotations @NotNull

and @Column(name="something", nullable=false)

do not match. More details here: Confusion: @NotNull vs @Column (nullable = false)

+4


source







All Articles