-1

This is actually not a question but maybe it's a bug.

I have an array of objects and I perform a loop in order to save it into the database:

for (ThresholdParams p : threshold.getThresholdParams()) {
    if (!p.getInitialValue().isNaN() && !p.getStepValue().isNaN()) {
            thresholdParamsRepository.save(p);
        }
    }

thresholdParamsRepository is an interface extending CrudRepository.

When the array do not have any invalid NaN value it works fine. If I perform a debug it stops everytime in the save sentence.

But if the 8th(for example) element of the array has a NaN value it performs all the for loop without performing the if and crashes because a mysql exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'NaN' in 'field list'

I used a break point to debug it in the save sentence but it skiped it after the first save. I debuged all and it perform all the saves until de 8th when it crashed. It did not commited the changes into the database.

I have solved it removing the elements with nan values but I do not know if it is a bug related to java or to spring data jpa.

This is the ThresholdParams class:

@Entity
@Table(name="threshold_params")
public class ThresholdParams {

    @EmbeddedId
    private ThresholdParamsId thresholdParamsId;

    @Column(name="step_value")
    private Float stepValue;

    @Column(name="initial_value", nullable= true)
    private Float initialValue;

    @ManyToOne
    @JoinColumn(name="contextSourceId",insertable=false, updatable= false)
    private ContextSource contextSource;

    @Column(name="is_enabled", columnDefinition="bit default 1")
    private boolean isEnabled;

    public boolean getIsEnabled() {
        return isEnabled;
    }

    public void setIsEnabled(boolean isEnabled) {
        this.isEnabled = isEnabled;
    }

    @ManyToOne
    @JoinColumn(name="thresholdId",insertable=false, updatable= false)
    private Threshold threshold;

}
dubes
  • 5,324
  • 3
  • 34
  • 47
dmance
  • 628
  • 1
  • 8
  • 26
  • 2
    Seeing the definition of the entity ThresholdParams would be interesting – wi2ard Oct 09 '18 at 12:20
  • How does `threshold` get its ThresholdParams? What does it hold? –  Oct 09 '18 at 12:24
  • @GabiM I pasted the thresholdparams class. And Codeer it get it throught some calculations based on data of another table. It would take long to explain it and I think it is not worth. – dmance Oct 09 '18 at 12:32
  • As at least `initialValue` is nullable, you should check for `null` in your `if` statement first. – Florian Albrecht Oct 09 '18 at 12:47
  • A slightly off-topic design question: when can the two Float float fields be NaN in your code? – wi2ard Oct 09 '18 at 13:29
  • @GabiM In some cases I have to perform a log2 operation and I need to handle this situation. – dmance Oct 09 '18 at 13:30
  • @dmance - There is a big problem with storing exact values in Float or Double because they are floating point types: https://stackoverflow.com/a/588014/2743585 . Consider using BigDecimal mapped to varchar columns instead, for example – wi2ard Oct 09 '18 at 14:39
  • Please post the full stack trace. Also please format it as code, not as a quote. – Jens Schauder Oct 10 '18 at 05:42

1 Answers1

-1

Since there is no column name 'NaN' That's why you're getting this exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'NaN' in 'field list'

You can check like this in your code:

for (ThresholdParams p : threshold.getThresholdParams()) {
    if (p !=null) {
            thresholdParamsRepository.save(p);
        }
    }

This will check the null value for your domain and will be saved if true.

I hope this will help.

Pawan Tiwari
  • 518
  • 6
  • 26
  • I don't see how this would help with NaN values? Also, the sentence `This will check the null value for your domain and will be saved if true.` doesn't make sense to me. – Florian Albrecht Oct 09 '18 at 12:50
  • NaN works in javaScript, not in java. And `p !=null` will check your domain object before saving it to the DB. – Pawan Tiwari Oct 09 '18 at 13:03
  • As in your exception, I can see that you're using Mysql `com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'NaN' in 'field list'` The link that you have posted here is for Oracle not for MySQL. – Pawan Tiwari Oct 09 '18 at 13:05
  • Sorry, I am not the OP. And Java is an Oracle product - that link is Java documentation, to prove that there *is* something like NaN in Java (that is exactly the OP's problem). – Florian Albrecht Oct 09 '18 at 13:08
  • As @FlorianAlbrecht pointed out, `isNan` is a Float method, part of the JDK, so it's not related to the database. The isNan check was not a null check, you can have NaN as result of an arithmetic operation – wi2ard Oct 09 '18 at 13:19
  • I think is there is something wrong because it is trying to find a column named NaN which of course is bad. I was looking the logs of hibernate and spring data jpa and it was trying to do the right update queries. I do not know where this error come from :S – dmance Oct 09 '18 at 13:31