Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I specify @Column( nullable = false )?

I have an entity annotated with @Entity.
If I am responsible for creating the CREATE TABLE scripts why should I specify @Column( nullable = false ) when I can create a column in the database with the NOT NULL keywords? Is there any example that shows the benefits of using this property in a field?

like image 522
Fagner Brack Avatar asked Mar 03 '13 01:03

Fagner Brack


People also ask

What does nullable false mean?

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.

Is @column annotation necessary?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What does the annotation @basic optional false mean?

It means, if you try to persist an entity with a null field it will throw an exception if it is marked as optional=false (without contacting the database) and the entity will not be added to the JPA persistence context.

What is nullable SQLAlchemy?

From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it's rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements.


2 Answers

Better error messages and error handling, especially if you also add the JSR303 @NotNull annotation.

If you create the column as NOT NULL but don't tell JPA it's not null, JPA will assume that null values are OK. When you try to save an object with nulls, it'll proceed to send that to the DB and you'll get a DB level error. This increases log spam in the database and it's much harder to determine from the error which column(s) of which table(s) were the problem, let alone map them back to their JPA names.

If you annotate them as not null, JPA will throw an exception before saving, avoiding DB log spam and usually giving you a better error. In particular, if your JPA provider supports JSR303 and either translates nullable=false into @NotNull internally or you've added @NotNull too, it'll give you a data structure you can examine to see exactly what fields of what objects were rejected for what reasons, along with customisable templated error messages.

That's why you should tell JPA about NOT NULL fields. That, and it's easier for others working on your code to understand without also having to read the DB schema.

like image 165
Craig Ringer Avatar answered Sep 23 '22 21:09

Craig Ringer


Additionally, if your column has nullable = false in @ManyToOne annotation, Hibernate does INNER JOIN query to the related table. nullable = true gives in result LEFT JOIN.

That is other difference.

like image 39
Adam Tychoniewicz Avatar answered Sep 23 '22 21:09

Adam Tychoniewicz