Of the three options, should I be using one or two or all three combined to prevent null values?
@NotNull
@Column(name = "SOME_VALUE", nullable = false)
@Basic(optional = false)
private String someValue;
Note that I don't consider this a duplicate of an existing question. I see many questions that ask about a subset of these three options but have yet to find one that asks about which of the three is appropriate to use in a modern JPA/Hibernate stack.
@NotNull
will generate an exception before a query is made. If you are using@Column(nullable = false)
and you are generating the tables using JPA, you will get an exception upon insert/update.@Basic(optional = false)
is also used during schema generation, so the exception will be generated upon insert/update due to constraint violation, same as with@Column(nullable = false)
. In general automated validation results in less-meaningful error messages than desired, so you might want to do it manually.