在Hibernate中使用原语或包装类? [英] Using primitives or wrapper class in Hibernate?
问题描述
使用Hibernate映射数据库。
Mapping database with Hibernate.
我们应该使用Double与@NotNull约束
或使用双基元类型。
最好的做法是什么? (使用Java 6)
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;
}
或
@Column(name = "price_after_tax")
public double getPriceAfterTax() {
return priceAfterTax;
}
非常感谢!
推荐答案
我认为你应该使用 Double
,因为它可以保持空值。所以,将来如果有任何机会决定使DB列无效,那么你是安全的。你只需要删除 @NotNull'。在这种情况下,你不能使用原始的
double`。
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 primitive
double` in this condition.
无论如何hibernate是对象关系映射,我总是坚持使用对象在我的实体bean。这是一个有争议的一点。
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.
这些原语甚至是一个逻辑上的问题,比如你不知道或没有价值 priceAfterTax
。如果您使用原语,默认值将始终为 0.0
。这表示没有税!。但是当我使用 Double
它有 null
默认值意味着我不知道该是什么税。
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.
这篇关于在Hibernate中使用原语或包装类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!