Using primitives or wrapper class in Hibernate?

随声附和 提交于 2019-12-10 14:58:13

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/10110081/using-primitives-or-wrapper-class-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!