• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Column Annotation with Nullable True : Best practice

 
Sangel Kapoor
Ranch Hand
Posts: 157
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So ,I am learning JPA/Hibernate and I encountered @Column Annotation.

@Column Annotation can have property "nullable" which can be set to true or false. This allows Hibernate to see whether nulls are to be stored or not.


Query : My query is regarding best Practice . We have JSR303 for Field Validations. Should not we use Validations instead of validating it like this on Persistence related Annotations. Should we use at both places ? One place ?


Just wanted to know expert opinion on this.

 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "nullable" attribute of the @Column notation isn't for Hibernate/JPA, it's for the database. Specifically, when using the option to generate a schema, it assures that the "NULLABLE" property is assigned to the corresponding database table column (field).

The @Nullable attribute is used by JPA - among other Java services - to ensure that the instance value of that Entity property (field) will bounce any processes that violate that constraint. The JPA "merge" function may be the most obvious, but some some frameworks allow Entity objects to be used directly in their GUI models (since they are POJOs) and in such cases, the higher-level services (like the JPA Controllers) can also stand watch and reject bad data long before the persistance operation would have to deal with it.

I recommend using both the @Column and the @Nullable just to be protected at all levels.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sangel Kapoor wrote:Query : My query is regarding best Practice . We have JSR303 for Field Validations. Should not we use Validations instead of validating it like this on Persistence related Annotations. Should we use at both places ? One place ?

@Column(nullable = false) is the JPA way of declaring a column to be not-null and is intended for indicating database schema details. @NotNull is a JSR 303 Bean Validation annotation. It has nothing to do with database constraints itself, but is intended for validation. Maybe some JPA providers can recognize JSR 303 annotations, but it's not guaranteed. Therefore it's recommended to use both.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected. The annotation is indeed @NotNull.

I use it copiously and Hibernate/JPA does honor it.

I also use the 'nullable' attribute of the @Column annotation. Because I'm a paranoid little
 
Sangel Kapoor
Ranch Hand
Posts: 157
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does that mean i should use it in a following way


@NotNull // JSR303
@Column(nullable = false) // Hibernate / JPA
private String firstName;

I still wonder what is the need for both.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sangel Kapoor wrote:So does that mean i should use it in a following way

Yes!

Sangel Kapoor wrote:I still wonder what is the need for both.

It's not required/needed to do both. If you are only interested in bean validation, using only @NotNull will probably do the job. But if you want the DDL schema being generated by your ORM, you might need to add nullable = false to the @Column annotation to add a NOT NULL constraint to the database column. It seems if you are using Hibernate as the ORM, it will will translate the constraints you have defined for your entities (like @NotNull) into mapping metadata out of the box. But that might not be the case for other ORMs (and therefore it might be more safe to add the nullable attribute.

Hope it helps!
Kind regards,
Roel
 
Sangel Kapoor
Ranch Hand
Posts: 157
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Bunch Roel and Tim :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic