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

I found the code in the entity class:

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

@NotNull values matter when @Id is already set ?

+5
source share
5 answers

@NotNullintended for verification, for example @Size. It defines the rules for a validation mechanism to verify that user input is normally entered. Running a check around these annotations does not necessarily indicate that the object is also a JPA object, but the two are often used together.

javax.validation , ( ), , .

+6

id / primary key , uniquely .

null.

+1

@Id

 * 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>.  

, . @NotNull @Id.

+1

, hibernate/orm. ?

, , NULL , @NotNull.

, null.

0

Yes, this will invalidate your id field, and you must specify it.

But if you want id to be auto-increment, delete it and add

@GeneratedValue (strategy = GenerationType.AUTO)

0
source

All Articles