Persistence @Column nullable = false can insert null
Solution 1
I think nullable is used if you generate the schema, using the implementation of the entitymanager. I don't know whether it is / has to be validated while persisting an entity as well.
Maybe it helps if you use the @NotNull annotation, but this is NOT plain JPA. It's defined in the JSR-303
There is a topic on Stackoverflow too that covers your question: Stackoverflow - Confusion notnull vs columnnullable false
EDIT: In the JPA 2.1 Specification, there is this section:
11.2.2.1 Column
The following elements of the Column annotation are used in schema generation:
name
unique
nullable
columnDefinition table
length (string-valued columns only) precision (exact numeric (decimal/numeric) columns only) scale (exact numeric (decimal/numeric) columns only) See section 11.1.9 for the rules that apply to these elements and column creation. The AttributeOverride annotation may be used to override column mappings.
As there is no other hint given, I assume the following: If a JPA-conform EntityManager CREATES the schema, it HAS to apply the nullable constraint on the specific column by using an aequivalent DB related constraint (e.g. notnull) When you persist an entity, it is NOT checked by the Entitymanager BUT by the underlying databse. So if the DB raises an error, the EntityManager propagates this error up to the caller.
If you create the table yourself without using the DB nullable constraint, then the entitymanager tries to persist the entity and gets NO error --> persist is okay althouh there are some null values that shouldn't be there.
Solution 2
if you directly do insert in database then Hibernate or any persistence provider would not ne able to control you. try to insert using persistence provider.
Comments
-
Mathew Rock almost 2 years
I want to do this column can´t be null but when I insert in database one register values null this allows me inserted. I read documentation and I don´t know why doesn´t work.
@Column(name="QWECOD", nullable = false) private String qwe;
THX
UPDATE: I´m using Toplink and java org.eclipse.persistence.eclipselink:2.4.2.
-
svz over 10 yearsShouldn't Hibernate set column restrictions to NOT NULL in DB? This way DB itself won't let you insert NULL values.
-
chrylis -cautiouslyoptimistic- over 10 years@svz Depends on whether you're using a Hibernate-generated DDL. Not everybody does.
-
Pankaj Sharma over 10 yearslet hbn create tables for you then only it would be possible
-
DominikM over 10 yearsIf you generate your db schema from entities it will be there most likely, but when you write entities with existing schema then nullable will be only hint for persistence provider
-
Vianney Dupoy de Guitard over 10 yearsNullable is used in the generation AND bean validation, when you want to persist something null. Plus
@NotNull
and(Nullable = false)
are the same. -
Andreas Aumayr over 10 yearsI cannot find any hint within the JPA 2.1 Specification that uses @NotNull - as it is NOT defined there. Also within the specification it is only vague defined (I'll have alook at the specs and updated my answer)
-
Vianney Dupoy de Guitard over 10 years
@NotNull
is not defined in JPA Specs of course, but Hibernate uses it as an alias fornullable=false
. Look at NotNullValidator: grepcode.com/file/repo1.maven.org/maven2/hibernate/… -
Andreas Aumayr over 10 yearsCorrect me if I am wrong, but I cannot see ANY hint in the question where the author says that he uses Hibernate ? I have added two relevant links in order to provide further information. Also I said that I am not SURE if it has to be used by the implementation of the entitymanager. Back to topic now: I'll update my answer and add the ONLY section that I have found within the JPA 2.1 specs that gives a hint where to use nullable.
-
Vianney Dupoy de Guitard over 10 yearsI talked about Hibernate because AFAIK, it's the only JPA Compliant persistence manager using
@NotNull
from JSR 303 ;). I justed wanted to clarify that@NotNull
=nullable = false
, so it would make no difference. -
Mathew Rock over 10 yearsI have other doubt, is better validate data in database(with restriction) or before persist? anyone know any article?