7

I found code in entity class:

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

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • 4
    If you're using a database autoincremented value or sequence then you shouldn't use the `@NotNull` annotation. It would throw an exception when you try to persist the entity. – greenkode Apr 03 '13 at 11:47
  • Hm... I simplify code, but original one have `@SequenceGenerator` and `@GeneratedValue` and there are no any errors in runtime on inserts... Seems that different libraries and versions behave differently. Thanks for interest. +1. – gavenkoa Apr 03 '13 at 11:50

5 Answers5

9

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

Jeff
  • 3,669
  • 1
  • 23
  • 33
  • Thanks +1. Most direct answer. – gavenkoa Apr 04 '13 at 05:19
  • This is the correct answer. The UI layer for example may require @NotNull attributes. Validation is a cross-cutting concern, so you expect it to show up in many places throughout the application. – setnoset Feb 27 '22 at 11:29
2

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

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Thanks. +1. I read http://docs.oracle.com/javaee/6/api/javax/validation/constraints/NotNull.html and http://docs.oracle.com/javaee/6/api/javax/persistence/Id.html but this doesn't help me in understanding... – gavenkoa Apr 03 '13 at 11:53
  • Allowing a null primary key would be a violation of the SQL spec. – Powerlord May 31 '16 at 18:46
1

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

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)

Nirbhay Mishra
  • 1,629
  • 3
  • 19
  • 35
0

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.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311