4

I have MySql database with several tables. Every field in each table is not nullable. That's why I am forced to use @NotNull annotation to each field in all my Java classes marked with @Entity. Do I really have to do this or is there a way to tell JPA/Hibernate to treat every field NotNullable on default?

edit: I am aware of @Column(name="something", nullable=false) too. But still, it doesn't solve any problem - you have to write nullable=false

kukis
  • 4,489
  • 6
  • 27
  • 50

1 Answers1

6

There are no such possibility. Not Nullable constraint is not what you always expect from a field, although it is used quite often. It is convenient when you can look at the attribute definition and tell everything out of it, without addressing to some high-level settings like "every field should be @NotNull".

It would be rather confusing to see such entity definition with this setting hidden elsewhere.

And, one more thing. @NotNull annotation and @Column(name="something", nullable=false) are not the same. More details here : Confusion: @NotNull vs @Column(nullable = false)

Community
  • 1
  • 1
AntonBerezin
  • 350
  • 1
  • 5