Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is any reason to put @NotNull annotation on @Id field in Java entity class

I found code in entity class:

@Id
@NotNull
@Column(name = "ID")
private Long id;

Is @NotNull annotation have value when @Id already set?

like image 901
gavenkoa Avatar asked Apr 03 '13 11:04

gavenkoa


1 Answers

@NotNull is for validation purposes, like @Size. It defines rules for a validation engine to check whether user input is ok. Doing validation around these annotation doesn't necessarily indicate that the object is also a JPA object but the two are often used together.

If you're using javax.validation instead of relying on failure at the DB level (constraint violations) to indicate null values, then you should use both annotations.

like image 162
Jeff Avatar answered Oct 13 '22 13:10

Jeff