Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using primitives or wrapper class in Hibernate?

Mapping database with Hibernate.

We should use Double with @NotNull constraint 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!

like image 208
phanhongphucit Avatar asked Apr 11 '12 16:04

phanhongphucit


1 Answers

I think you should use Double as it can hold even null value. So, in future if by any chance you decides make the DB column null-able, then you are safe. You just need to remove the @NotNull'. You can not use the primitivedouble` in this condition.

In any case hibernate is Object Relational Mapping, I would always stick to using Objects in my entity beans. It's a debatable point through.

There is even a logical issue with the primitives, say you don't know or don't have a value for priceAfterTax. If you use the primitive, the default value will be always there 0.0. Which says that there is no tax!. But when I use Double it has null by defaults means I don't know what the tax is.

like image 74
ManuPK Avatar answered Sep 29 '22 05:09

ManuPK