The answer above is totally correct and I just type extra info.
Use nullable types (boxed primitive types like Integer, Double, etc.) for optional values. It depends on your model if it make sense for particular value to be optional. If it has to have default values, you can create @PreX callbacks (for example @PreLoad) or override getter for returning primitive type with default value instead.
Use primitive types for required value. It has some great benefits if you distinguish among these two approaches like:
- If you let hibernate generate DDL, it will automatically create not null constraints for primitive types.
- If you do mapping among web dto objects and entities, you have implicit NPE which indicates problem in your code and that behaviour will not make your data inconsistent
There is one interesting thing tho. If you use Lombok library and like to use @Getter and @Setter on entities and you control SelectBeforeInsert behaviour with Persistable interface then you should always use big boxed type like Integer, because you canout do genericity with primitive type like Persistable. If you use Persistable + lombok's @Getter + your ID is named like "id" then you have implicit override getter method for getId. It is just symbiose among lombok + jpa entity.