I don't understand at all the difference between these annotations. Could you please explain me if this is redundant?
@NotNull
@Basic(optional = false)
@Column(nullable = false)
private int userId;
Thanks in advance.
I don't understand at all the difference between these annotations. Could you please explain me if this is redundant?
@NotNull
@Basic(optional = false)
@Column(nullable = false)
private int userId;
Thanks in advance.
@NotNull : This annotation you are defining that your code will not except a null parameter, if you were to provide a null parameter, the annotation would throw a RuntimeException.
@Basic : This annotation signifies that an attribute is to be persisted and a standard mapping is to be used. It has parameters which allow you to specify whether the attribute is to be lazily loaded and whether it's nullable.
@Column allows you to specify the name of the column in the database to which the attribute is to be persisted.
Here are the difference that I noticed.
@NotNull comes from javax.validation JSR303 it define if the property of the bean can be null, set this indicate that property can not be null, this are called constraints and are verified in validation.validate() method.
@Basic(optional = false) comes from javax.persistence JSR317, there are Basic types and collection Types it indicate the logical model can be null or not set optional to false indicate that the propery can not be null at jpa logical model. Also allows you to declare the fetching strategy for a property
@Column(nullable = false) comes from javax.persistence JSR317, it is related to physical mapping with the database (DDL) telling to the database that the property can not be null in the database. (NOT NULL statement in table creation)
As you can see first one works with Validation API, second and third apply to JPA API but one in the logical and the other in the physical mode.