Topic
1 reply Latest Post - ‏2012-08-14T15:06:35Z by leealber
SystemAdmin
SystemAdmin
45 Posts
ACCEPTED ANSWER

Pinned topic JPA - send a null or 0.0 value to double field

‏2012-08-14T14:12:50Z |
Hello,

I am wondering if someone can help her.

I am using JPA to entity manager to send data from xml into a database. One of the fields in the database is set as a double. This field is nullable.

When loading the data into the database. The parsers loads in the last non null value for all null value fields. Is their a way I can set a null value for the field and if the field is empty - to load the default value.

Currently I tried setting the value in the JPA getter and setter class but it did not help
//putting in 0.0 here to help with null values issues for float element
@Column(columnDefinition="double precision default '0.0'",name="field")
//the 0.0 is not working here, so going to try it in the column definition
private double field=0.0;

Thanks in advance
Updated on 2012-08-14T15:06:35Z at 2012-08-14T15:06:35Z by leealber
  • leealber
    leealber
    19 Posts
    ACCEPTED ANSWER

    Re: JPA - send a null or 0.0 value to double field

    ‏2012-08-14T15:06:35Z  in response to SystemAdmin
    The attribute "field" of type double is a primitive type. From Java perspective, it will always has a value and default to 0.0. So JPA will always store 0.0 to the database even if it is not set.

    You may want to define "field" as Double type, which you can have null value set in the database.

    The tricky part is to set the value to 0.0 on load if the database column is null. You may be able to simulate it in the getField() methods and return 0.0 if "field" is null.

    Hope this help.

    Albert Lee.