@Column(nullable = false)
- The @Column annotation is part of the JPA specification, and you already use all required dependencies.So no need to add any extra dependency in your project.
- Hibernate doesn’t perform any validation if you annotate an attribute with @Column(nullable = false). This annotation only adds a not null constraint to the database column, if Hibernate creates the database table definition. The database then checks the constraint, when you insert or update a record.
- The @Column(nullable = false) annotation only adds a not null constraint to the table definition. Hibernate or any other framework will not perform any validation on the entity attribute. Hibernate just executes the SQL INSERT/UPDATE statement, and the database will validate the constraint. If the entity attribute is null, the SQL statement will fail.
@NotNull annotation
- The @NotNull annotation is part of the BeanValidation specification. You need to add a dependency to the Hibernate Validator project
- The @NotNull annotation triggers a validation by the Bean Validation implementation when a pre-update or a pre-persist lifecycle event gets triggered. So, the validation happens within your Java application.
- The @NotNull annotation tells your Bean Validation implementation to check that the attribute is not null. This happens when the pre-update or pre-persist lifecycle event gets triggered. If the validation fails, Hibernate will not execute any SQL statement.
Which one is better?
You should always use the @NotNull annotation, which is defined by the Bean Validation specification. It configures a validation step that gets performed before Hibernate executes the SQL statement.