I found code in entity class:
@Id
@NotNull
@Column(name = "ID")
private Long id;
Is @NotNull annotation have value when @Id already set?
I found code in entity class:
@Id
@NotNull
@Column(name = "ID")
private Long id;
Is @NotNull annotation have value when @Id already set?
@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.
@Id
is used to
* Specifies the primary key of an entity.
* The field or property to which the <code>Id</code> annotation is applied
* should be one of the following types: any Java primitive type;
* any primitive wrapper type;
* <code>String</code>;
* <code>java.util.Date</code>;
* <code>java.sql.Date</code>;
* <code>java.math.BigDecimal</code>;
* <code>java.math.BigInteger</code>.
So it doesn't takes care of null values. To prevent null values @NotNull
is used along with @Id
.
Since id / primary key
is the most important field in the table, it uniquely
identify a row in the table.
So it shouldn't be null
.
Yes this will make your id field not nullable and you have to give it mandatory.
But if you want id to autoincrement then remove it and add
@GeneratedValue(strategy=GenerationType.AUTO)
My understanding is this is for static analysis and has nothign to do with hibernate/orm. Is it the intellij specific annotation ?
So at compile time you can be warned when you are about to assign NULL to a field that has been annotated as @NotNull.
And any code that is dependent knows that the returned value will never be null.