简体   繁体   English

grails,当字段包含null时从db加载域会导致setter失败

[英]grails, loading domain from db when fields contains null causes setters to fail

This problem is about some fields that are declared double and often is null in the database. 此问题与某些字段声明为double且在数据库中通常为null有关。 Using findAllBy to receive a list then fails if some of the fields are null. 如果某些字段为空,则使用findAllBy接收列表失败。 I tried with changing the type to Double but then I get problems when I do arithmetics on them. 我尝试将类型更改为Double,但是在对它们进行算术运算时遇到了问题。 If the value is null the Double can't decide which converter it should use. 如果该值为null,则Double无法确定应使用哪个转换器。 I don't know if there is any way to convert nulls in the domain before they are set. 我不知道是否有任何方法可以在域中设置空值之前将其转换。 Example of error: 错误示例:

Caused by: org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.buffer.ProdBuffer.makeInquiry

You should always use Double in your entities/domains; 您应该始终在实体/域中使用Double unless you have a nullable: false constraint at work, even then it's a good practice, nonetheless. 除非您在工作中有一个nullable: false约束,否则即便如此,这也是一个好习惯。

However, you need to handle null by yourself in your calculations, probably by making that 0 or 1, you know it better. 但是,您需要在计算中自己处理null ,可能将其设为0或1会更好。 For more suggestions, post the code snippet. 有关更多建议,请发布代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM