0

In JPA, if a column isn't nullable, it must have a default value. So if I say

@Column(nullable=false)
Boolean isExpired;

Would it default to true or false? What if it's an Integer or a Long or a String? Is there any way to specify this?

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
MiguelMunoz
  • 4,548
  • 3
  • 34
  • 51
  • Note that if your column is non-null, you should generally use the primitive instead of the wrapper. – chrylis -cautiouslyoptimistic- Jan 07 '21 at 04:23
  • For your reference https://stackoverflow.com/questions/15181632/why-should-i-specify-column-nullable-false https://www.baeldung.com/hibernate-notnull-vs-nullable#:~:text=The%20%40Column(nullable%20%3D%20false)%20Annotation&text=It's%20used%20mainly%20in%20the,to%20the%20particular%20database%20column. – Sibin Rasiya Jan 07 '21 at 10:31

1 Answers1

0

@Column(nullable=false) simply means that you should always supply value to that field when inserting or updating. If you want to specify a default value you can always add this:

 @Column(columnDefinition = "boolean default false")

But as per your question, if you will not define a specific value for that field (through columnDefinition), it will still be null and will prompt an error during DB insert or update if it's null.

Donato Amasa
  • 846
  • 2
  • 6
  • 22