7

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.

Mathew Rock
  • 989
  • 2
  • 14
  • 32

3 Answers3

5

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.

Community
  • 1
  • 1
Andreas Aumayr
  • 1,006
  • 1
  • 11
  • 17
  • Nullable is used in the generation AND bean validation, when you want to persist something null. Plus `@NotNull` and `(Nullable = false)` are the same. – Vianney Dupoy de Guitard Oct 18 '13 at 08:05
  • I 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) – Andreas Aumayr Oct 18 '13 at 08:07
  • `@NotNull` is not defined in JPA Specs of course, but Hibernate uses it as an alias for `nullable=false`. Look at NotNullValidator: http://grepcode.com/file/repo1.maven.org/maven2/hibernate/hibernate-annotations/3.1beta4/org/hibernate/validator/NotNullValidator.java#NotNullValidator – Vianney Dupoy de Guitard Oct 18 '13 at 08:17
  • Correct 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. – Andreas Aumayr Oct 18 '13 at 08:24
  • I 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. – Vianney Dupoy de Guitard Oct 18 '13 at 08:46
  • I have other doubt, is better validate data in database(with restriction) or before persist? anyone know any article? – Mathew Rock Oct 18 '13 at 11:18
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.

Pankaj Sharma
  • 1,833
  • 1
  • 17
  • 22
0

Probably in the base you already have that field with nulls In these cases you must change the nullity of the field manually MySql Query:

 alter table [table] modify [column] [[Column_Type] ej: vachar] not null;