简体   繁体   中英

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.

I'm using hibernate with annotations to help save data to a mysql db. 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.

When reading from a file, if I receive an empty string, then I set the corresponding "Float" field to be null.

For inserts that do not have this field as null, the inserts work properly and records get inserted into the db. However when this field is null, then I get a NPE. I have check before the call to check if the managing class is not null, and it isn't. 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.

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.

@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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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