如何在数据库表中保存一个空的字段映射为double?
问题描述:
在Hibernate Layer中,数据库列价格使用Double类型映射。然而,由于业务逻辑,我必须为价格处理空白的领域。没有价格并且价格为0.0,在buiness逻辑中有两个不同的含义。如何处理这个?如何在数据库表中保存一个空的字段映射为double?
boolean day1 = true;
boolean day2 = true;
boolean day3 = true;
boolean day4 = true;
boolean day5 = true;
boolean day6 = true;
boolean day7 = true;
//double ogPrice = 10.99;
OrderGuideProduct orderGuideProduct = new OrderGuideProduct();
orderGuideProduct.setDay1(day1);
orderGuideProduct.setDay2(day2);
orderGuideProduct.setDay3(day3);
orderGuideProduct.setDay4(day4);
orderGuideProduct.setDay5(day5);
orderGuideProduct.setDay6(day6);
orderGuideProduct.setDay7(day7);
// this is where I need to set the empty value
orderGuideProduct.setOgPrice();
答
我建议在这种情况下使用BigDecimal
而非double
。前者是可空的(因此可以从0.0区分开来),并且应该被Hibernate识别。
答
拥有Double而不是double有助于我。
查找已知并且很少使用的SQL关键字,称为“NULL”。 –
另外浮点数与固定小数点不同。不是所有的小数点都是浮点数。 –
你为什么使用双打? http://*.com/questions/3730019/why-not-use-double-or-float-to-represent-currency –