1

I readed this. But it didnt gave ma a clear answer of usage.

When I create table and columns.

Case1:

@Column(nullable = false, length = 14)
@NotNull
private String something;

Case2:

@Column(length = 14)
@NotNull
private String something;

Isn't it the same? I mean @NotNull will throw exception earlier so is there a reason to do nullable = false column?

degath
  • 1,530
  • 4
  • 31
  • 60

1 Answers1

5

@Column is database related. If you auto-create a database schema from your Java code, the nullable=false will make sure to create a NOT NULL column, which should be useful if there are other applications accessing your database. Also, your app may check this constraint just before sending data to the database.

@NotNull is Java related. Java will throw exceptions when this attribute is not set. No database context necessary.

Both annotations sound alike, but serve two different purposes. There might be libraries which automatically infer one annotation from the other, but you can always use both annotations. When you do, @NotNull will be checked BEFORE @Column.

Mick
  • 954
  • 7
  • 17