简体   繁体   English

如何为空浮点字段使用带注释的休眠

[英]How to use Hibernate with Annotation for Null Float Field

I've been fighting this one a while this morning, and didn't find any good leads with google or stack overflow. 今天早上我一直在努力争取这一点,但没有找到与Google或堆栈溢出有关的好的线索。

I'm using hibernate with annotations to help save data to a mysql db. 我正在使用带有注释的休眠模式来帮助将数据保存到mysql数据库中。 The mysql table allows for a float field to be set to null, and input from a file can be empty string, which implies a null should be saved to the db. mysql表允许将float字段设置为null,并且文件输入可以为空字符串,这意味着应将null保存到db。

When reading from a file, if I receive an empty string, then I set the corresponding "Float" field to be null. 从文件读取时,如果收到空字符串,则将相应的“ Float”字段设置为null。

For inserts that do not have this field as null, the inserts work properly and records get inserted into the db. 对于不具有该字段为null的插入,插入将正常工作,并且记录将插入到db中。 However when this field is null, then I get a NPE. 但是,当该字段为空时,我得到了NPE。 I have check before the call to check if the managing class is not null, and it isn't. 我在调用之前已进行检查,以检查管理类是否不为null,并且不是。 I also put a break point in the first line of the method used to insert and try to step in while in debug mode, and I can't get any further. 我还在调试模式下的第一行中插入了一个断点,并尝试在调试模式下介入,此后我一无所获。 Based on this, I believe there are things behind the scene that are causing the NPE. 基于此,我相信幕后有一些事情导致了NPE。

From reading Persistence @Column nullable = false can insert null I was trying to use the nullable annotation, but it looks like nullable is used for schema generation. 从阅读Persistence @Column nullable = false可以插入null,我试图使用nullablenullable注释,但看起来nullablenullable可用于模式生成。

@Column(name = "SomeFloatField", nullable = true)

My question is pretty straight forward, how do you get hibernate to allow "null" for float values to be saved to the db? 我的问题很简单,您如何进入休眠状态以允许将float值“空”保存到数据库?

Found the issue... which is pretty bad... but I'll put a post here, in the off chance anyone else goes through the same frustration I went through. 找到了问题……这很糟糕……但我会在这里贴一个帖子,以免其他任何人遇到与我同样的挫败感。

I'm new to hibernate, so when I was developing I started with float then migrated it to Float when I saw that the underlying db could accept nulls. 我是休眠的新手,所以在开发时,我从float开始,然后在看到基础数据库可以接受空值时将其迁移到Float Sadly in the insert method, its signature had float instead of Float which means when I passed the null that it blew up, which makes perfect sense. 可悲的是,在insert方法中,它的签名是float而不是Float ,这意味着当我传递null时,它就炸掉了,这是很合理的。

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

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