Using primitives or wrapper class in Hibernate?

Map a database to Hibernate.

We should use Double with the restriction @NotNull Or use the double primitive type instead. What is the best practice? (Using Java 6)

@Column(name = "price_after_tax", nullable=false) @NotNull public Double getPriceAfterTax() { return priceAfterTax; } 

OR

 @Column(name = "price_after_tax") public double getPriceAfterTax() { return priceAfterTax; } 

Many thanks!

+5
source share
1 answer

I think you should use Double , since it can even contain a null value. So, in the future, if you decide to make the DB column null, then you are safe. You just need to remove @NotNull'. You can not use the primitive @NotNull'. You can not use the primitive double` in this condition.

In any case, hibernate is a relational mapping of objects, I will always stick to using objects in my beans entity. This is a moot point.

There is even a logical problem with primitives, for example, you do not know or do not matter for priceAfterTax . If you use a primitive, the default value will always be 0.0 . That says no tax! . But when I use Double , it is null by default, which means I donโ€™t know what tax is.

+6
source

Source: https://habr.com/ru/post/914308/


All Articles