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.
The best way to avoid Hibernate's attempts at setting null values to primitives is to use Wrapper classes (Integer, Long, Double...); and especially, if you need to tack on a column or 2 to an existing table. Auto-boxing is your friend.
For each row in table, a column can contain a value or NULL which indicates "no value." If a column is declared NOT NULL, it must always contain a non-NULL value; NULL is not allowed. Columns can have a default value.
The @Column(nullable = false) Annotation It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.
@Column(nullable = false) is the JPA way of declaring a column to be not-null.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With